diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb
index f32c19b..9812204 100644
--- a/app/controllers/books_controller.rb
+++ b/app/controllers/books_controller.rb
@@ -19,6 +19,7 @@ def edit
def create
@book = Book.new(book_params)
+ @book.user = current_user
respond_to do |format|
if @book.save
@@ -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
diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb
index 5b04ca7..c4450bb 100644
--- a/app/controllers/rentals_controller.rb
+++ b/app/controllers/rentals_controller.rb
@@ -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
@@ -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
diff --git a/app/models/ability.rb b/app/models/ability.rb
index b0b00a9..d60ac17 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -12,6 +12,7 @@ def initialize(user)
end
can :read, Book
+ can :create, Book
can :new, Rental
can :create, Rental
@@ -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|
diff --git a/app/models/book.rb b/app/models/book.rb
index ae43b89..72a0cc2 100644
--- a/app/models/book.rb
+++ b/app/models/book.rb
@@ -19,5 +19,6 @@ class Book < ActiveRecord::Base
end
has_many :rentals
+ belongs_to :user
end
diff --git a/app/models/user.rb b/app/models/user.rb
index c3e7fe3..b69f105 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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
diff --git a/app/views/books/_books_row.html.erb b/app/views/books/_books_row.html.erb
new file mode 100644
index 0000000..592ae37
--- /dev/null
+++ b/app/views/books/_books_row.html.erb
@@ -0,0 +1,12 @@
+
+ <%= link_to book.name, book %> |
+ <%= book.author %> |
+ <%= book.sku %> |
+ <%= book.state %> |
+
+ <%= 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? %>
+ |
+ <%= link_to('Edit', edit_book_path(book)) if can? :edit, book %> |
+ <%= 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 %> |
+
\ No newline at end of file
diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb
index 648753f..dd0b042 100644
--- a/app/views/books/index.html.erb
+++ b/app/views/books/index.html.erb
@@ -11,21 +11,7 @@
<% @books.each do |book| %>
-
- <%= link_to book.name, book %> |
- <%= book.author %> |
- <%= book.sku %> |
- <%= book.state %> |
-
- <% if can? :create, Rental and book.active? %>
- <%= link_to 'Rent', rentals_path(sku: book.sku), method: :post %>
- <% end %>
- |
- <% if can? :manage, book %>
- <%= link_to 'Edit', edit_book_path(book) %> |
- <%= link_to 'Delete', book, method: :delete, data: { confirm: 'Are you sure you want to remove this item from library?' } %> |
- <% end %>
-
+ <%= render partial: 'books_row', locals: { book: book } %>
<% end %>
diff --git a/app/views/rentals/destroy.js.erb b/app/views/rentals/destroy.js.erb
new file mode 100644
index 0000000..9f2978c
--- /dev/null
+++ b/app/views/rentals/destroy.js.erb
@@ -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 %>
\ No newline at end of file
diff --git a/app/views/rentals/new.js.erb b/app/views/rentals/new.js.erb
new file mode 100644
index 0000000..af66eb6
--- /dev/null
+++ b/app/views/rentals/new.js.erb
@@ -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 %>
\ No newline at end of file
diff --git a/db/migrate/20160307165205_add_user_to_book.rb b/db/migrate/20160307165205_add_user_to_book.rb
new file mode 100644
index 0000000..d75d7bb
--- /dev/null
+++ b/db/migrate/20160307165205_add_user_to_book.rb
@@ -0,0 +1,5 @@
+class AddUserToBook < ActiveRecord::Migration
+ def change
+ add_reference :books, :user, index: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 03e586c..37af427 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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"
@@ -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"
diff --git a/db/seeds.rb b/db/seeds.rb
index 023fac4..a940436 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -52,4 +52,4 @@
b.author = 'Lauren Groff Van Thomson Pierson Writerson'
b.sku = '12352'
b.state = :active
-end
+end
\ No newline at end of file