diff --git a/Gemfile b/Gemfile index bf629fa..56b86c8 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,11 @@ gem 'rails', '~> 5.2.2' gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 3.11' +gem 'email_validator' +gem 'pry-rails', :group => :development +gem 'devise', '~> 4.6', '>= 4.6.1' +gem 'carrierwave', '~> 0.10.0' +gem 'mini_magick', '~> 4.3' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets diff --git a/Gemfile.lock b/Gemfile.lock index adbb6d4..1920f42 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,7 @@ GEM archive-zip (0.12.0) io-like (~> 0.3.0) arel (9.0.0) + bcrypt (3.1.12) bindex (0.5.0) bootsnap (1.4.1) msgpack (~> 1.0) @@ -60,13 +61,27 @@ GEM rack-test (>= 0.6.3) regexp_parser (~> 1.2) xpath (~> 3.2) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) chromedriver-helper (2.1.0) archive-zip (~> 0.10) nokogiri (~> 1.8) + coderay (1.1.2) concurrent-ruby (1.1.4) crass (1.0.4) + devise (4.6.1) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0, < 6.0) + responders + warden (~> 1.2.3) + email_validator (2.0.1) + activemodel erubi (1.8.0) execjs (2.7.0) ffi (1.10.0) @@ -78,6 +93,7 @@ GEM jbuilder (2.8.0) activesupport (>= 4.2.0) multi_json (>= 1.2) + json (2.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -90,7 +106,11 @@ GEM marcel (0.3.3) mimemagic (~> 0.3.2) method_source (0.9.2) + mime-types (3.2.2) + mime-types-data (~> 3.2015) + mime-types-data (3.2018.0812) mimemagic (0.3.3) + mini_magick (4.9.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) @@ -99,7 +119,13 @@ GEM nio4r (2.3.1) nokogiri (1.10.1) mini_portile2 (~> 2.4.0) + orm_adapter (0.5.0) pg (1.1.4) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.9) + pry (>= 0.10.4) public_suffix (3.0.3) puma (3.12.0) rack (2.0.6) @@ -134,6 +160,9 @@ GEM rb-inotify (0.10.0) ffi (~> 1.0) regexp_parser (1.3.0) + responders (2.4.1) + actionpack (>= 4.2.0, < 6.0) + railties (>= 4.2.0, < 6.0) ruby_dep (1.5.0) rubyzip (1.2.2) sass (3.7.3) @@ -169,6 +198,8 @@ GEM thread_safe (~> 0.1) uglifier (4.1.20) execjs (>= 0.3.0, < 3) + warden (1.2.8) + rack (>= 2.0.6) web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) @@ -187,10 +218,15 @@ DEPENDENCIES bootsnap (>= 1.1.0) byebug capybara (>= 2.15) + carrierwave (~> 0.10.0) chromedriver-helper + devise (~> 4.6, >= 4.6.1) + email_validator jbuilder (~> 2.5) listen (>= 3.0.5, < 3.2) + mini_magick (~> 4.3) pg (>= 0.18, < 2.0) + pry-rails puma (~> 3.11) rails (~> 5.2.2) sass-rails (~> 5.0) diff --git a/app/assets/javascripts/books.js b/app/assets/javascripts/books.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/books.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/home.js b/app/assets/javascripts/home.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/home.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/books.scss b/app/assets/stylesheets/books.scss new file mode 100644 index 0000000..9fab565 --- /dev/null +++ b/app/assets/stylesheets/books.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Books controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/home.scss b/app/assets/stylesheets/home.scss new file mode 100644 index 0000000..7131aac --- /dev/null +++ b/app/assets/stylesheets/home.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..6045188 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,84 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +th { + padding-bottom: 5px; +} + +td { + padding: 0 5px 7px; +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} + +label { + display: block; +} diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb new file mode 100644 index 0000000..53a9dbb --- /dev/null +++ b/app/controllers/books_controller.rb @@ -0,0 +1,102 @@ +class BooksController < ApplicationController + before_action :authenticate_user!,except: [:index, :show] + before_action :set_book, only: [:show, :edit, :update, :destroy] + # before_action :current_user, only: [:edit, :update, :destroy] + + # GET /books + # GET /books.json + def index + + if current_user + @books = current_user.books + else + redirect_to home_path + end + # binding.pry + end + + + # GET /books/1 + # GET /books/1.json + def show + user_edit + end + + # GET /books/new + def new + @book = Book.new + end + + # GET /books/1/edit + def edit + user_edit + # binding.pry + end + + + # POST /books + # POST /books.json + def create + # @book = Book.new(book_params) + @book = current_user.books.build(book_params) + + respond_to do |format| + if @book.save + format.html { redirect_to @book, notice: 'Book was successfully created.' } + format.json { render :show, status: :created, location: @book } + else + format.html { render :new } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + + + + # PATCH/PUT /books/1 + # PATCH/PUT /books/1.json + def update + respond_to do |format| + if @book.update(book_params) + format.html { redirect_to @book, notice: 'Book was successfully updated.' } + format.json { render :show, status: :ok, location: @book } + else + format.html { render :edit } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /books/1 + # DELETE /books/1.json + def destroy + @book.destroy + respond_to do |format| + format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_book + @book = Book.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def book_params + params.require(:book).permit(:title, :author, :description, :image, :category) + end + + def user_edit + if (user_signed_in? && (current_user.id == @book.user_id)) + @can_edit = true + else + @can_edit = false + end + end + + + +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 0000000..5e293fd --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,13 @@ +class HomeController < ApplicationController + def index + @books = Book.all + my_book + end + def my_book + if (user_signed_in?) + @has_books = true + else + @has_books = false + end + end +end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb new file mode 100644 index 0000000..4b9311e --- /dev/null +++ b/app/helpers/books_helper.rb @@ -0,0 +1,2 @@ +module BooksHelper +end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 0000000..23de56a --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 0000000..0b73b38 --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,8 @@ +class Book < ApplicationRecord + belongs_to :user + mount_uploader :image, ImageUploader +end + + + + diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..2252442 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,11 @@ +class User < ApplicationRecord + + has_many :books, dependent: :destroy + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable + validates :email, presence: true, format: /\w+@\w+\.{1}[a-zA-Z]{2,}/ +end + + diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb new file mode 100644 index 0000000..4861131 --- /dev/null +++ b/app/uploaders/image_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class ImageUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :resize_to_fit => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w(jpg jpeg gif png) + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/books/_book.json.jbuilder b/app/views/books/_book.json.jbuilder new file mode 100644 index 0000000..c241f8c --- /dev/null +++ b/app/views/books/_book.json.jbuilder @@ -0,0 +1,3 @@ +json.extract! book, :id, :title, :author, :description, :image, :category, :created_at, :updated_at +json.url book_url(book, format: :json) + diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb new file mode 100644 index 0000000..f237428 --- /dev/null +++ b/app/views/books/_form.html.erb @@ -0,0 +1,45 @@ + + +<%= form_with(model: book, local: true) do |form| %> + <% if book.errors.any? %> +
<%= notice %>
+ +| Title | +Author | +Description | +Image | +Category | ++ | ||
|---|---|---|---|---|---|---|---|
| <%= book.title %> | +<%= book.author %> | +<%= book.description %> | +<%= book.image.url %> | +<%= book.category %> | +<%= link_to 'Show', book %> | +<%= link_to 'Edit', edit_book_path(book) %> | +<%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ Title: + <%= @book.title %> +
+ ++ Author: + <%= @book.author %> +
+ ++ DEscription: + <%= @book.description %> +
+ ++Image: +<% if @book.image.url != nil %> + <%= image_tag @book.image.url %> +<%else%> +<%= image_tag 'http://insignia.sd23.bc.ca/Library/images/cover/book.png' %> +<%end%> +
+ ++ Category: + <%= @book.category %> +
+ + +<%= link_to 'Back', books_path %> + +<% if @can_edit %> +<%= link_to "Edit", edit_book_path(@book) %> +<% end %> diff --git a/app/views/books/show.json.jbuilder b/app/views/books/show.json.jbuilder new file mode 100644 index 0000000..c1e5174 --- /dev/null +++ b/app/views/books/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "books/book", book: @book diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb new file mode 100644 index 0000000..43b3bd4 --- /dev/null +++ b/app/views/home/index.html.erb @@ -0,0 +1,23 @@ +| Title | +
|---|
| <%= link_to book.title, book %> | +
<%= notice %>
+<%= alert %>
+ <% if user_signed_in? %> + <%= link_to 'logout', destroy_user_session_path, method: :delete %> + <%= link_to "Change your password", edit_user_registration_path %> + <% else %> + <%= link_to "login", new_user_session_path %> + <% end %> <%= yield %>