diff --git a/app/controllers/book_controller.rb b/app/controllers/book_controller.rb index 098222f..8565b48 100644 --- a/app/controllers/book_controller.rb +++ b/app/controllers/book_controller.rb @@ -25,15 +25,17 @@ class BookController < ApplicationController redirect_to '/login', notice: 'You need login to do the action.' return end - book = Book.find_by_id params[:id] - if book.users.count <= book.current_borrowers.count - @msg = 'Sorry, there is no more book for lend.' - else - #book.borrowers << @current_user - @current_user.borrow book - @msg = 'Borrowed success.' - end - redirect_to :back, alert: @msg + #book = Book.find_by_id params[:id] + #if book.users.count <= book.current_borrowers.count + # @msg = 'Sorry, there is no more book for lend.' + #else + # #book.borrowers << @current_user + # @current_user.borrow book + # @msg = 'Borrowed success.' + #end + instance = BookInstance.find_by_id params[:instance_id].to_s + @current_user.borrow instance + redirect_to :back, alert: 'Borrow book success.' end def new diff --git a/app/models/user.rb b/app/models/user.rb index 1180e8b..72f2167 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,8 +18,7 @@ class User < ActiveRecord::Base User.create name: name, avatar: avatar end - def borrow(book) - instance = book.available_instance + def borrow(instance) unless instance == nil borrow_records.create user_id: id, book_instance_id: instance.id, borrow_date: DateTime.now end diff --git a/spec/models/book_instance_spec.rb b/spec/models/book_instance_spec.rb index dcd94dc..c967b29 100644 --- a/spec/models/book_instance_spec.rb +++ b/spec/models/book_instance_spec.rb @@ -13,7 +13,7 @@ describe BookInstance do end it 'ensure when someone borrowed this book,borrowed? should return true' do - @user.borrow @book + @user.borrow @instance @instance.borrowed?.should == true end diff --git a/spec/models/book_spec.rb b/spec/models/book_spec.rb index 0cb3d79..5e61590 100644 --- a/spec/models/book_spec.rb +++ b/spec/models/book_spec.rb @@ -13,9 +13,9 @@ describe :Book do it 'when a book has two instances,ensure there is still one when another has been borrowed' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id - @user.borrow @book + @user.borrow @instance @book.available_instance.should == instance_1 - @user.borrow @book + @user.borrow instance_1 @book.available_instance.should == nil end end @@ -27,7 +27,7 @@ describe :Book do it 'ensure there should be one borrower' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id - @user.borrow @book + @user.borrow @instance @book.current_borrowers.count.should == 1 @book.current_borrowers[0].should == @user end @@ -37,7 +37,7 @@ describe :Book do it 'ensure current_borrowers count equals total_borrowers count,when there isn"t some return books "' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id @book.total_borrowers.count.should == 0 - @user.borrow @book + @user.borrow instance_1 @book.current_borrowers.count.should == 1 @book.total_borrowers.count.should == 1 @book.total_borrowers[0].name.should == @user.name @@ -47,8 +47,8 @@ describe :Book do it 'ensure total_borrowers will count the user who returned the book' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id user_1 = User.create name:'Tom' - @user.borrow @book - user_1.borrow @book + @user.borrow @instance + user_1.borrow instance_1 @user.should_not == nil @user.return_book @instance @book.total_borrowers.count.should == 2 @@ -62,16 +62,16 @@ describe :Book do it 'ensure total available instance should be 1 after borrowed' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id @book.total_available_instances.count.should == 2 - @user.borrow @book + @user.borrow instance_1 @book.total_available_instances.count.should == 1 end it 'ensure total available instances should be two when someone return back his borrowed books' do instance_1 = BookInstance.create book_id: @book.id, user_id: @user.id @book.total_available_instances.count.should == 2 - @user.borrow @book + @user.borrow instance_1 @book.total_available_instances.count.should == 1 - @user.return_book @instance + @user.return_book instance_1 @book.total_available_instances.count.should == 2 end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2e85661..43994f2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -7,13 +7,10 @@ describe :User do end describe 'user borrow a book' do - it 'ensure user can"t borrow a book, when there is no book instance' do - @user.borrow @book - @user.books.empty?.should be_true - end + it 'ensure user can borrow a existed book' do instance = BookInstance.create book_id: @book.id, user_id: @user.id - @user.borrow @book + @user.borrow instance @user.borrowed_and_not_returned_books.empty?.should be_false @user.borrowed_and_not_returned_books.count.should == 1 end @@ -23,7 +20,7 @@ describe :User do instance = BookInstance.create book_id: book_1.id, user_id: user_1.id instance_2 = BookInstance.create book_id: book_1.id, user_id: user_1.id user_1.borrowed_and_not_returned_books.count.should == 0 - user_1.borrow book_1 + user_1.borrow instance user_1.borrowed_and_not_returned_books.empty?.should be_false user_1.borrowed_and_not_returned_books.count.should == 1 @@ -34,7 +31,7 @@ describe :User do describe :borrowed_and_not_returned_books do it 'ensure return the book I borrowed and didn"t return back."' do instance = BookInstance.create book_id: @book.id, user_id: @user.id - @user.borrow @book + @user.borrow instance book_instances = @user.borrowed_and_not_returned_books book_instances.size.should == 1 book_instances[0].should == instance @@ -45,7 +42,7 @@ describe :User do describe :return_book do it 'ensure user return book,the borrowed books" count should decrease one"' do instance = BookInstance.create book_id: @book.id, user_id: @user.id - @user.borrow @book + @user.borrow instance @user.return_book instance @user.borrowed_and_not_returned_books.count.should == 0 end