From: Ira W. Snyder Date: Fri, 23 Nov 2007 09:48:53 +0000 (-0800) Subject: Add MaxVideos and MaxGames RentablePolicies X-Git-Tag: turned-in~52 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=9218748c58d552432e7bd4a34383589eb17ac331;p=cs356-p2-videostore.git Add MaxVideos and MaxGames RentablePolicies Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index 704e38b..a3b61bb 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -58,6 +58,21 @@ class PurchaseController < ApplicationController return end + # Check Rentable Policies + @maxvideos = RentablePolicy.find_by_name("MaxVideos") + if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value + flash[:error] = "#{@maxvideos.description} LIMIT REACHED" + redirect_to :action => :rent_begin + return + end + + @maxgames = RentablePolicy.find_by_name("MaxGames") + if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value + flash[:error] = "#{@maxgames.description} LIMIT REACHED" + redirect_to :action => :rent_begin + return + end + # Check out the item checkout = Coitem.new checkout.customer = @customer diff --git a/app/models/customer.rb b/app/models/customer.rb index aba5044..e56eafe 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -7,6 +7,32 @@ class Customer < ActiveRecord::Base validates_presence_of :name, :email, :phone, :address validates_numericality_of :debt + def checked_out_videos + coitems = Coitem.find_all_by_customer_id(id) + video_count = 0 + + for item in coitems + if item.rentable.class == Video + video_count += 1 + end + end + + return video_count + end + + def checked_out_games + coitems = Coitem.find_all_by_customer_id(id) + game_count = 0 + + for item in coitems + if item.rentable.class == Game + game_count += 1 + end + end + + return game_count + end + protected def validate diff --git a/db/development.sqlite3 b/db/development.sqlite3 index f7ee02e..8470ab9 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ