Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 74 additions & 31 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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/
17 changes: 6 additions & 11 deletions app/controllers/books_controller.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

50 changes: 50 additions & 0 deletions app/controllers/copies_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions app/controllers/copy_controller.rb

This file was deleted.

Empty file modified app/helpers/books_helper.rb
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions app/helpers/copies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CopiesHelper
end
2 changes: 0 additions & 2 deletions app/helpers/copy_helper.rb

This file was deleted.

6 changes: 6 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/models/copy.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Copy < ApplicationRecord
belongs_to :book
validates :borrower, presence: true
validates :due_date, presence: true
end
13 changes: 13 additions & 0 deletions app/views/books/_book_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= form_with model: @book do |f| %>

<% %w[title author isbn].each do |input| %>
<p><%= f.label input %>:
<%= f.text_field input %></p>
<% end %>

<%= f.submit %>

<br/>

<%= link_to 'Return', @book.id ? book_path(@book) : books_path %>
<% end -%>
19 changes: 1 addition & 18 deletions app/views/books/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
<h1>Add new book</h1>

<%= form_with model: @book do |f| %>
<p><%= f.label :title %>:
<%= f.text_field :title %></p>

<p><%= f.label :author %>:
<%= f.text_field :author %></p>

<p><%= f.label :isbn %>:
<%= f.text_field :isbn %></p>

<%= f.submit "Change" %>
<% end -%>

<br/>

<%= link_to 'Back', {:action => 'index'} %>
<%= render 'book_form', :object => @book %>
6 changes: 2 additions & 4 deletions app/views/books/index.html.erb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
<h1>All Books</h1>
<ul>
<% @books.each { |book| %>
<li><%= book.title %>
(<%= link_to "View & edit", :action => 'edit', :id => book %>) </li>
<li><%= button_to "delete", book, :method => :delete %></li>
<li><%= link_to book.title, book_path(book) %></li>
<br/>
<% } %>
</ul>
<%end %>

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


19 changes: 1 addition & 18 deletions app/views/books/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
<h1>Add new book</h1>

<%= form_with model: @book do |f| %>
<p><%= f.label :title %>:
<%= f.text_field :title %></p>

<p><%= f.label :author %>:
<%= f.text_field :author %></p>

<p><%= f.label :isbn %>:
<%= f.text_field :isbn %></p>

<%= f.submit "Create" %>
<% end -%>

<br/>

<%= link_to 'Back', {:action => 'index'} %>
<%= render 'book_form' %>
10 changes: 10 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1><%= @book.title %> </h1>
<p>By <%= @book.author %> </p>
<p>ISBN: <%= @book.isbn %></p>

<%= link_to "Edit", :action => 'edit' %><br/><br/>
<%= button_to "delete", :method => :delete %>

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

<%= link_to 'Back to all books', books_path %>
18 changes: 18 additions & 0 deletions app/views/copies/_copy_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<%= form_with model: @copy do |f| %>

<p><%= f.label :borrower %>:
<%= f.text_field :borrower %></p>

<p>
<%= f.label :due_date %>:
<%= f.date_field :due_date %>
</p>

<%= f.submit %>

<br/><br/>

<%= link_to 'Back', book_copies_path(@copy.book_id || @book) %>

<% end -%>
1 change: 1 addition & 0 deletions app/views/copies/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'copy_form' %>
21 changes: 21 additions & 0 deletions app/views/copies/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<% if @copies.blank? %>
<p>There are no copies of this book in the library</p>

<% else %>
<h1>All Copies of <%= @book.title %></h1>

<% @copies.each { |copy| %>
<p>
Borrowed by <strong><%=copy.borrower %></strong>
with a return date of <strong><%=copy.due_date %></strong>
(<%= link_to 'edit', edit_copy_path(copy), :action => :edit %>)
</p>

<% } %>

<%end %>

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

<br/>
<%= link_to 'Back', book_path(@book) %>
1 change: 1 addition & 0 deletions app/views/copies/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'copy_form' %>
2 changes: 1 addition & 1 deletion config/environments/production.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion config/initializers/inflections.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
7 changes: 5 additions & 2 deletions config/routes.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions db/migrate/20200909101937_create_copies.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down