Lots of stuff, I got too tired to keep perfect revision history
[cs356-p2-videostore.git] / app / models / bonus_purchase.rb
1 class BonusPurchase < Purchase
2   belongs_to :rentable
3   belongs_to :customer
4
5   validates_presence_of :rentable_id
6   validates_numericality_of :rentable_id
7
8   def self.last_bonus_date(customer,bonus_type,since_date)
9     bonuses = BonusPurchase.find_all_by_customer_id(customer, :conditions => ["date >= ?", since_date], :order => 'date DESC')
10     puts "*** BONUSES.length: #{bonuses.length} ***"
11     for bonus in bonuses
12       puts "#{bonus.rentable.class.to_s} == #{bonus_type.to_s} => #{bonus.rentable.class.to_s == bonus_type.to_s}"
13       if bonus.rentable.class.to_s == bonus_type.to_s
14         return bonus.date
15       end
16     end
17
18     # Unable to find a last bonus, no bonuses in period
19     return since_date
20   end
21       
22   protected
23   def validate
24     errors.add(:rentable_id, "is not in the database") if rentable.nil?
25   end
26 end
27