Skip to content

Commit

Permalink
Adding books by all users + UI enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejfuhrer committed Mar 7, 2016
1 parent fe2ef12 commit ae9b9f1
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 23 deletions.
2 changes: 2 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def edit

def create
@book = Book.new(book_params)
@book.user = current_user

respond_to do |format|
if @book.save
Expand All @@ -42,6 +43,7 @@ def update
def destroy
@book.remove
respond_to do |format|
format.js
format.html { redirect_to books_url, notice: 'Book was successfully removed.' }
end
end
Expand Down
17 changes: 12 additions & 5 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class RentalsController < ApplicationController
def destroy
@rental.return
respond_to do |format|
format.js
format.html { redirect_to dashboard_path, notice: 'Book was successfully returned.' }
end
end
Expand All @@ -23,12 +24,18 @@ def create
if not book
redirect_to new_rental_path, alert: "Book with SKU [#{sku}] has not been found."
else
r = Rental.create book: book, user: current_user
if not r.validate
flash_error_for r, :cannot_be_created
render 'rentals/new'
@rental = Rental.create book: book, user: current_user
if not @rental.validate
flash_error_for @rental, :cannot_be_created
respond_to do |format|
format.js { render 'rentals/new' }
format.html { render 'rentals/new' }
end
else
redirect_to dashboard_path, notice: "Book [#{book.name}] has been successfully rented"
respond_to do |format|
format.js { render 'rentals/new' }
format.html { redirect_to dashboard_path, notice: "Book [#{book.name}] has been successfully rented" }
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(user)
end

can :read, Book
can :create, Book

can :new, Rental
can :create, Rental
Expand All @@ -23,7 +24,10 @@ def initialize(user)

if user.role.manager?
can :manage, Book
can :manage, Rental
else
can :manage, Book do |book|
(book.user == user)
end
end

can [:read, :update], User do |u|
Expand Down
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Book < ActiveRecord::Base
end

has_many :rentals
belongs_to :user

end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class User < ActiveRecord::Base
end

has_many :rentals
has_many :books

ROLES = [:admin, :manager, :user]
enumerize :role, in: ROLES, default: :user
Expand Down
12 changes: 12 additions & 0 deletions app/views/books/_books_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<tr id="book-row-<%= book.id %>">
<td><%= link_to book.name, book %></td>
<td><%= book.author %></td>
<td><%= book.sku %></td>
<td><span class="<%= get_bootstrap_class_for_state book.state %>"><%= book.state %></span></td>
<td>
<%= link_to('Rent', rentals_path(sku: book.sku, format: :js), method: :post, remote: true) if can? :create, Rental and book.active? %>
<%= link_to('Return', rental_path(book.rentals.active.first, format: :js), method: :delete, remote: true) if can? :destroy, Rental and book.rented? %>
</td>
<td><%= link_to('Edit', edit_book_path(book)) if can? :edit, book %></td>
<td><%= link_to('Delete', book_path(book, format: :js), method: :delete, data: { confirm: 'Are you sure you want to remove this item from library?' }, remote: true) if can? :destroy, book %></td>
</tr>
16 changes: 1 addition & 15 deletions app/views/books/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@

<tbody>
<% @books.each do |book| %>
<tr>
<td><%= link_to book.name, book %></td>
<td><%= book.author %></td>
<td><%= book.sku %></td>
<td><span class="<%= get_bootstrap_class_for_state book.state %>"><%= book.state %></span></td>
<td>
<% if can? :create, Rental and book.active? %>
<%= link_to 'Rent', rentals_path(sku: book.sku), method: :post %>
<% end %>
</td>
<% if can? :manage, book %>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Delete', book, method: :delete, data: { confirm: 'Are you sure you want to remove this item from library?' } %></td>
<% end %>
</tr>
<%= render partial: 'books_row', locals: { book: book } %>
<% end %>
</tbody>
</table>
Expand Down
5 changes: 5 additions & 0 deletions app/views/rentals/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if @rental.errors.any? %>
alert('This book cannot be returned.');
<% else %>
$('#book-row-<%= @rental.book.id %>').replaceWith("<%= j render(partial: 'books/books_row', locals: {book: @rental.book}).html_safe %>");
<% end %>
5 changes: 5 additions & 0 deletions app/views/rentals/new.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if @rental.errors.any? %>
alert('This book cannot be rented.');
<% else %>
$('#book-row-<%= @rental.book.id %>').replaceWith("<%= j render(partial: 'books/books_row', locals: {book: @rental.book}).html_safe %>");
<% end %>
5 changes: 5 additions & 0 deletions db/migrate/20160307165205_add_user_to_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserToBook < ActiveRecord::Migration
def change
add_reference :books, :user, index: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160307152511) do
ActiveRecord::Schema.define(version: 20160307165205) do

create_table "books", force: :cascade do |t|
t.text "name"
Expand All @@ -20,8 +20,11 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "state"
t.integer "user_id"
end

add_index "books", ["user_id"], name: "index_books_on_user_id"

create_table "rentals", force: :cascade do |t|
t.integer "user_id"
t.integer "book_id"
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
b.author = 'Lauren Groff Van Thomson Pierson Writerson'
b.sku = '12352'
b.state = :active
end
end

0 comments on commit ae9b9f1

Please sign in to comment.