Small Cleanups + Merchandise Search
[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     for bonus in bonuses
11       if bonus.rentable.class.to_s == bonus_type.to_s
12         return bonus.date
13       end
14     end
15
16     # Unable to find a last bonus, no bonuses in period
17     return since_date
18   end
19
20   protected
21   def validate
22     errors.add(:rentable_id, "is not in the database") if rentable.nil?
23   end
24 end
25