Add MaxVideos and MaxGames RentablePolicies
[cs356-p2-videostore.git] / app / models / customer.rb
1 class Customer < ActiveRecord::Base
2   has_many :coitems
3   has_many :bitems
4   has_many :merchandise_purchases
5   has_many :rentable_purchases
6
7   validates_presence_of :name, :email, :phone, :address
8   validates_numericality_of :debt
9
10   def checked_out_videos
11     coitems = Coitem.find_all_by_customer_id(id)
12     video_count = 0
13
14     for item in coitems
15       if item.rentable.class == Video
16         video_count += 1
17       end
18     end
19
20     return video_count
21   end
22
23   def checked_out_games
24     coitems = Coitem.find_all_by_customer_id(id)
25     game_count = 0
26
27     for item in coitems
28       if item.rentable.class == Game
29         game_count += 1
30       end
31     end
32
33     return game_count
34   end
35
36   protected
37
38   def validate
39     errors.add(:debt, "should be non-negative") if debt.nil? || debt < 0.00
40   end
41
42 end