public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
# app/controllers/books/base_controller.rb
class BooksController < ApplicationController
respond_to :html
before_filter :find_resource, :only => [:show, :edit, :update, :destroy]
 
def index
@books = context.page params[:page]
end
 
def show
end
 
def new
@book = context.new
end
 
def edit
end
 
def create
@book = context.new(params[:book])
 
# Save the last donor to speed up the process
session[:donor_id] = @book.donor_id
 
## Should be handled by Book class
# unless @book.isbn.blank?
# @book.attributes = OCLC.find_by_isbn(@book.isbn)
# @book.attributes = AMZN.find_by_isbn(@book.isbn)
# end
 
if @book.save
flash[:notice] = 'You successfully added a book.'
redirect_to :action => 'show', :id => @book.id
else
render :new
end
end
 
def update
if @book.update_attributes(params[:book])
flash[:notice] = case @book.recommendation
when 'keep'
"The book's recommendation has been updated to: keep."
when 'toss'
"The book's recommendation has been updated to: toss."
when 'undecided'
"The book's recommendation has been updated to: undecided."
else
"You successfully updated the book."
end
 
redirect_to :action => 'show', :id => @book.id
else
render :edit
end
end
 
def destroy
if @book.destroy
flash[:notice] = 'You successfully deleted the book.'
redirect_to :action => 'index'
else
flash[:error] = 'There was a problem removing the book. Please try again.'
redirect_to :action => 'index'
end
end
 
protected
def find_resource
@book = context.find(params[:id])
end
 
def current_context
if params[:bin_id]
Bin.find(params[:bin_id]).books
elsif params[:donor_id]
Donor.find(params[:donor_id]).books
else
Book
end
end
 
def context
@_context ||= current_scope ? current_context.send(current_scope) : current_context
end
 
def current_scope
Book.scopes.keys.include?(params[:scope].try(:to_sym)) ? params[:scope] : nil
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.