X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fcontrollers%2Fcoitem_controller.rb;h=152f40b17f50ae651049f26c61fe7db61dbba81d;hb=refs%2Ftags%2Fturned-in;hp=d93a4b0b319d9f1c70a1742a839c34b6fc854ee9;hpb=233a6b122bc4aed44a0babee0797d2dba6da5b06;p=cs356-p2-videostore.git diff --git a/app/controllers/coitem_controller.rb b/app/controllers/coitem_controller.rb index d93a4b0..152f40b 100644 --- a/app/controllers/coitem_controller.rb +++ b/app/controllers/coitem_controller.rb @@ -1,12 +1,12 @@ class CoitemController < ApplicationController + layout "admin" # Make sure that the user has logged in before they can take any # action on checked out items before_filter :authorize def index - list - render :action => 'list' + render :action => 'index' end # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) @@ -21,19 +21,8 @@ class CoitemController < ApplicationController @coitem = Coitem.find(params[:id]) end - def new - @coitem = Coitem.new - end - - def create - @coitem = Coitem.new(params[:coitem]) - if @coitem.save - flash[:notice] = 'Coitem was successfully created.' - redirect_to :action => 'list' - else - render :action => 'new' - end - end + # We should never create new checked out items via the web interface, so remove + # the new and create methods. def edit @coitem = Coitem.find(params[:id]) @@ -49,48 +38,52 @@ class CoitemController < ApplicationController end end - def destroy - Coitem.find(params[:id]).destroy - redirect_to :action => 'list' - end + # We should never delete a checked out item directly via the web interface, so + # remove the destroy method. # Awesome, paginating overdue list, ordered by customer def overdue - @coitem_pages, @coitems = paginate :coitems, :per_page => 50, :conditions => "due_date < DATE('NOW', 'LOCALTIME')", :order => "customer_id" - render :action => 'list' + @coitem_pages, @coitems = paginate :coitems, :per_page => 50, :conditions => ["due_date < ?", Time.now.to_date], :order => "customer_id" + render :action => 'overdue' end - def return - render :action => 'return' + def filterbycustomerid + query = params[:id] + @coitem_pages, @coitems = paginate :coitems, :per_page => 20, :conditions => ["customer_id = ?", query] + render :action => 'list' end - def return_validate - rentable_id = params[:rentable_id] - @rentable = Rentable.find_by_id(rentable_id) - - if @rentable.nil? - flash[:error] = "Unable to find this rentable" - redirect_to :action => :return - return - end - - @coitem = Coitem.find_by_rentable_id(rentable_id) - if @coitem.nil? - flash[:error] = "This item is not checked out!" - redirect_to :action => :return - return - end - - # Check if the item is overdue - if @coitem.overdue? - @coitem.customer.debt += @coitem.late_fee - @coitem.customer.save + def return + if request.post? + rentable_id = params[:rentable_id] + @rentable = Rentable.find_by_id(rentable_id) + + if @rentable.nil? + flash[:notice] = "Unable to find this rentable" + redirect_to :action => :return + return + end + + @coitem = Coitem.find_by_rentable_id(rentable_id) + if @coitem.nil? + flash[:notice] = "This item is not checked out!" + redirect_to :action => :return + return + end + + # Check if the item is overdue + if @coitem.overdue? + @coitem.customer.debt += @coitem.late_fee + @coitem.customer.save + end + + # Delete the row + @coitem.destroy + + flash[:notice] = "Successfully returned item" + redirect_to :action => :return, :method => :get + else + render :action => 'return' end - - # Delete the row - @coitem.destroy - - flash[:notice] = "Successfully returned item" - redirect_to :action => :return end end