b9d902481d62d2ec435820d6fe883e31f7e4c3b6
[cs356-p2-videostore.git] / app / models / coitem.rb
1 class Coitem < ActiveRecord::Base
2   belongs_to :customer
3   belongs_to :rentable
4
5   # Make sure the user typed in the ids
6   validates_presence_of :customer_id
7   validates_presence_of :rentable_id
8
9   # Make sure the user typed in numbers
10   validates_numericality_of :customer_id
11   validates_numericality_of :rentable_id
12
13   # Make sure this rentable was not already checked out
14   validates_uniqueness_of :rentable_id
15
16   # Make sure the associated rentable and customer are valid and exist
17   validates_associated :customer
18   validates_associated :rentable
19
20   def overdue?
21     return due_date < Time.now.to_date
22   end
23
24   def late_fee
25     days_late = Time.now.to_date - (due_date)
26     return VideoPolicy.find_by_day(8).fee * days_late.to_i
27   end
28
29   protected
30   def validate
31     errors.add(:customer_id, "does not exist is the database") if customer.nil?
32     errors.add(:rentable_id, "does not exist in the database") if rentable.nil?
33   end
34 end