Add MaxVideos and MaxGames RentablePolicies
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 09:48:53 +0000 (01:48 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 09:48:53 +0000 (01:48 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/controllers/purchase_controller.rb
app/models/customer.rb
db/development.sqlite3

index 704e38b..a3b61bb 100644 (file)
@@ -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
index aba5044..e56eafe 100644 (file)
@@ -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
index f7ee02e..8470ab9 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ