diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4f83dbd..b6aeb78 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -27,14 +27,21 @@ - - + + - - + + + + + + + + + @@ -42,6 +49,10 @@ + + + + @@ -87,6 +98,11 @@ + + + + + @@ -293,54 +309,62 @@ - + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + + - - + + - - + + - - + + - + @@ -353,13 +377,32 @@ - + - + + + + + + file://$PROJECT_DIR$/app/controllers/copies_controller.rb + 36 + + + + + + + + + + + \ No newline at end of file diff --git a/app/assets/javascripts/copy.coffee b/app/assets/javascripts/copies.coffee similarity index 100% rename from app/assets/javascripts/copy.coffee rename to app/assets/javascripts/copies.coffee diff --git a/app/assets/stylesheets/copy.scss b/app/assets/stylesheets/copies.scss similarity index 64% rename from app/assets/stylesheets/copy.scss rename to app/assets/stylesheets/copies.scss index 9539dc6..3a74ec5 100755 --- a/app/assets/stylesheets/copy.scss +++ b/app/assets/stylesheets/copies.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the copy controller here. +// Place all the styles related to the copies controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb old mode 100644 new mode 100755 index 66dbf59..21a85f1 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -3,14 +3,14 @@ def index @books = Book.all end - def show - @book = Book.find(params[:id]) - end - def new @book = Book.new end + def show + @book = Book.find(params[:id]) + end + def create @book = Book.new(book_params) @@ -32,21 +32,16 @@ def edit def update @book = Book.find(params[:id]) - if @book.update_attributes(book_param) + if @book.update_attributes(book_params) redirect_to action: 'index' else render action: 'edit' end end - def book_param - params.require(:book).permit(:title, :price, :subject_id, :description) - end - - # I was expecting to call this 'delete' since that is what 'method' calls - # Why is it destroy instead? Is there any way I could have guessed that? def destroy Book.find(params[:id]).destroy redirect_to action: 'index' end end + diff --git a/app/controllers/copies_controller.rb b/app/controllers/copies_controller.rb new file mode 100755 index 0000000..660be33 --- /dev/null +++ b/app/controllers/copies_controller.rb @@ -0,0 +1,50 @@ +class CopiesController < ApplicationController + before_action :find_book + + def find_book + if params[:book_id].blank? + @copy = Copy.find(params[:id]) + @book = Book.find(@copy.book_id) + else + @book = Book.find(params[:book_id]) + end + end + + def index + @copies = @book.copies + end + + def new + @copy = @book.copies.new + end + + def create + @copy = @book.copies.new(copy_params) + if @copy.save + redirect_to action: 'index' + else + puts 'Did not create' + render action: 'new' + end + end + + def copy_params + params.require(:copy).permit(:borrower, :due_date) + end + + def edit; end + + def update + + if @copy.update_attributes(copy_params) + redirect_to book_copies_path(@book) + else + render action: 'edit' + end + end + + def destroy + Copy.find(params[:id]).destroy + redirect_to action: book_copies_path(@book) + end +end diff --git a/app/controllers/copy_controller.rb b/app/controllers/copy_controller.rb deleted file mode 100755 index b37704c..0000000 --- a/app/controllers/copy_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class CopyController < ApplicationController -end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb old mode 100644 new mode 100755 diff --git a/app/helpers/copies_helper.rb b/app/helpers/copies_helper.rb new file mode 100755 index 0000000..735e977 --- /dev/null +++ b/app/helpers/copies_helper.rb @@ -0,0 +1,2 @@ +module CopiesHelper +end diff --git a/app/helpers/copy_helper.rb b/app/helpers/copy_helper.rb deleted file mode 100755 index 18368a7..0000000 --- a/app/helpers/copy_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module CopyHelper -end diff --git a/app/models/book.rb b/app/models/book.rb index 03d56b4..ec0086c 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -1,2 +1,8 @@ class Book < ApplicationRecord + has_many :copies, dependent: :destroy + + # TODO: handle better if validation fails + validates :title, presence: true + validates :author, presence: true + validates :isbn, presence: true end diff --git a/app/models/copy.rb b/app/models/copy.rb index 6e2f4d1..4a1a0ac 100644 --- a/app/models/copy.rb +++ b/app/models/copy.rb @@ -1,2 +1,5 @@ class Copy < ApplicationRecord + belongs_to :book + validates :borrower, presence: true + validates :due_date, presence: true end diff --git a/app/views/books/_book_form.html.erb b/app/views/books/_book_form.html.erb new file mode 100755 index 0000000..bfa066d --- /dev/null +++ b/app/views/books/_book_form.html.erb @@ -0,0 +1,13 @@ +<%= form_with model: @book do |f| %> + + <% %w[title author isbn].each do |input| %> +

<%= f.label input %>: + <%= f.text_field input %>

+ <% end %> + + <%= f.submit %> + +
+ + <%= link_to 'Return', @book.id ? book_path(@book) : books_path %> +<% end -%> \ No newline at end of file diff --git a/app/views/books/edit.html.erb b/app/views/books/edit.html.erb index b83a92d..f5daca0 100755 --- a/app/views/books/edit.html.erb +++ b/app/views/books/edit.html.erb @@ -1,18 +1 @@ -

Add new book

- -<%= form_with model: @book do |f| %> -

<%= f.label :title %>: - <%= f.text_field :title %>

- -

<%= f.label :author %>: - <%= f.text_field :author %>

- -

<%= f.label :isbn %>: - <%= f.text_field :isbn %>

- - <%= f.submit "Change" %> -<% end -%> - -
- -<%= link_to 'Back', {:action => 'index'} %> \ No newline at end of file +<%= render 'book_form', :object => @book %> diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb old mode 100644 new mode 100755 index 6c9d431..ff76cb5 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -5,14 +5,12 @@

All Books

    <% @books.each { |book| %> -
  • <%= book.title %> - (<%= link_to "View & edit", :action => 'edit', :id => book %>)
  • -
  • <%= button_to "delete", book, :method => :delete %>
  • +
  • <%= link_to book.title, book_path(book) %>

  • <% } %>
<%end %> -

<%= link_to "Add new Book", {:action => 'new' }%>

+

<%= link_to "Add new Book", :action => :new %>

diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb index ed96693..774a15a 100755 --- a/app/views/books/new.html.erb +++ b/app/views/books/new.html.erb @@ -1,18 +1 @@ -

Add new book

- -<%= form_with model: @book do |f| %> -

<%= f.label :title %>: - <%= f.text_field :title %>

- -

<%= f.label :author %>: - <%= f.text_field :author %>

- -

<%= f.label :isbn %>: - <%= f.text_field :isbn %>

- - <%= f.submit "Create" %> -<% end -%> - -
- -<%= link_to 'Back', {:action => 'index'} %> +<%= render 'book_form' %> diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb new file mode 100755 index 0000000..4f4885a --- /dev/null +++ b/app/views/books/show.html.erb @@ -0,0 +1,10 @@ +

<%= @book.title %>

+

By <%= @book.author %>

+

ISBN: <%= @book.isbn %>

+ +<%= link_to "Edit", :action => 'edit' %>

+<%= button_to "delete", :method => :delete %> + +

<%= link_to "View Copies", book_copies_path(@book) %>

+ +<%= link_to 'Back to all books', books_path %> \ No newline at end of file diff --git a/app/views/copies/_copy_form.html.erb b/app/views/copies/_copy_form.html.erb new file mode 100755 index 0000000..de56061 --- /dev/null +++ b/app/views/copies/_copy_form.html.erb @@ -0,0 +1,18 @@ + +<%= form_with model: @copy do |f| %> + +

<%= f.label :borrower %>: + <%= f.text_field :borrower %>

+ +

+ <%= f.label :due_date %>: + <%= f.date_field :due_date %> +

+ + <%= f.submit %> + +

+ + <%= link_to 'Back', book_copies_path(@copy.book_id || @book) %> + +<% end -%> \ No newline at end of file diff --git a/app/views/copies/edit.html.erb b/app/views/copies/edit.html.erb new file mode 100755 index 0000000..cc1408e --- /dev/null +++ b/app/views/copies/edit.html.erb @@ -0,0 +1 @@ +<%= render 'copy_form' %> \ No newline at end of file diff --git a/app/views/copies/index.html.erb b/app/views/copies/index.html.erb new file mode 100755 index 0000000..23e27f5 --- /dev/null +++ b/app/views/copies/index.html.erb @@ -0,0 +1,21 @@ +<% if @copies.blank? %> +

There are no copies of this book in the library

+ +<% else %> +

All Copies of <%= @book.title %>

+ + <% @copies.each { |copy| %> +

+ Borrowed by <%=copy.borrower %> + with a return date of <%=copy.due_date %> + (<%= link_to 'edit', edit_copy_path(copy), :action => :edit %>) +

+ + <% } %> + +<%end %> + +

<%= link_to "Add new Copy", :action => 'new' %>

+ +
+<%= link_to 'Back', book_path(@book) %> \ No newline at end of file diff --git a/app/views/copies/new.html.erb b/app/views/copies/new.html.erb new file mode 100755 index 0000000..9f09f1f --- /dev/null +++ b/app/views/copies/new.html.erb @@ -0,0 +1 @@ +<%= render 'copy_form' %> diff --git a/config/environments/production.rb b/config/environments/production.rb old mode 100644 new mode 100755 index 71feec7..64b20fa --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -6,7 +6,7 @@ # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers - # and those relying on copy on write to perform better. + # and those relying on copies on write to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb old mode 100644 new mode 100755 index ac033bf..9f616c2 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -1,5 +1,7 @@ # Be sure to restart your server when you modify this file. - +ActiveSupport::Inflector.inflections do |inflect| + inflect.irregular 'copy', 'copies' +end # Add new inflection rules using the following format. Inflections # are locale specific, and you may define rules for as many different # locales as you wish. All of these examples are active by default: diff --git a/config/routes.rb b/config/routes.rb old mode 100644 new mode 100755 index f47f3aa..e65a60b --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do - resources :books - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + resources :books do + resources :copies, shallow: true + end end +# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + diff --git a/db/migrate/20200909101937_create_copies.rb b/db/migrate/20200909101937_create_copies.rb old mode 100644 new mode 100755 index bd5f4f0..82b26e7 --- a/db/migrate/20200909101937_create_copies.rb +++ b/db/migrate/20200909101937_create_copies.rb @@ -1,8 +1,8 @@ class CreateCopies < ActiveRecord::Migration[5.2] def change create_table :copies do |t| - t.string :Borrower, null: false - t.date :DueDate, null: false + t.string :borrower, null: false + t.date :due_date, null: false t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index cac5dbb..cede1ce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -21,8 +21,8 @@ end create_table "copies", force: :cascade do |t| - t.string "Borrower", null: false - t.date "DueDate", null: false + t.string "borrower", null: false + t.date "due_date", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "book_id"