From: Ira W. Snyder Date: Fri, 23 Nov 2007 10:06:36 +0000 (-0800) Subject: Add RentablePolicy for MaxOverdueVideos and MaxOverdueGames X-Git-Tag: turned-in~51 X-Git-Url: https://www.irasnyder.com/gitweb/?p=cs356-p2-videostore.git;a=commitdiff_plain;h=93daca12cd904df11a8faadda08e487e9a4de8b7;hp=9218748c58d552432e7bd4a34383589eb17ac331 Add RentablePolicy for MaxOverdueVideos and MaxOverdueGames Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index a3b61bb..ebb08cd 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -73,6 +73,20 @@ class PurchaseController < ApplicationController return end + @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos") + if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value + flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED" + redirect_to :action => :rent_begin + return + end + + @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames") + if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value + flash[:error] = "#{@maxoverduegames.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 e56eafe..673cffd 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -33,6 +33,32 @@ class Customer < ActiveRecord::Base return game_count end + def overdue_videos + coitems = Coitem.find_all_by_customer_id(id) + overdue_video_count = 0 + + for item in coitems + if item.rentable.class == Video and item.overdue? + overdue_video_count += 1 + end + end + + return overdue_video_count + end + + def overdue_games + coitems = Coitem.find_all_by_customer_id(id) + overdue_game_count = 0 + + for item in coitems + if item.rentable.class == Game and item.overdue? + overdue_game_count += 1 + end + end + + return overdue_game_count + end + protected def validate diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 8470ab9..7294671 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ