X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fmodels%2Fcoitem.rb;h=b9d902481d62d2ec435820d6fe883e31f7e4c3b6;hb=01114330b962728805d3c43f2f1a1423f2bd66b3;hp=e77f5737b397db52cad892897f5ceb3cfc3ffd92;hpb=4930bd87730032786d9cc7f3d5c1d7cd1f5091bc;p=cs356-p2-videostore.git diff --git a/app/models/coitem.rb b/app/models/coitem.rb index e77f573..b9d9024 100644 --- a/app/models/coitem.rb +++ b/app/models/coitem.rb @@ -1,12 +1,34 @@ class Coitem < ActiveRecord::Base - has_one :customer - has_one :rentable + belongs_to :customer + belongs_to :rentable + # Make sure the user typed in the ids validates_presence_of :customer_id validates_presence_of :rentable_id + # Make sure the user typed in numbers + validates_numericality_of :customer_id + validates_numericality_of :rentable_id + + # Make sure this rentable was not already checked out validates_uniqueness_of :rentable_id + # Make sure the associated rentable and customer are valid and exist validates_associated :customer validates_associated :rentable + + def overdue? + return due_date < Time.now.to_date + end + + def late_fee + days_late = Time.now.to_date - (due_date) + return VideoPolicy.find_by_day(8).fee * days_late.to_i + end + + protected + def validate + errors.add(:customer_id, "does not exist is the database") if customer.nil? + errors.add(:rentable_id, "does not exist in the database") if rentable.nil? + end end