X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=app%2Fmodels%2Fcoitem.rb;h=5d0a2129398d611722fb126fcb145eef2b756dc6;hb=50e2ce05003eb4a56d1daa8e030a2f477aeb59bc;hp=a6bc1ade6d518950999332e5965571bb492449d1;hpb=be6d865110840adfc90f572a4cf14f62bc4cf31e;p=cs356-p2-videostore.git diff --git a/app/models/coitem.rb b/app/models/coitem.rb index a6bc1ad..5d0a212 100644 --- a/app/models/coitem.rb +++ b/app/models/coitem.rb @@ -2,11 +2,37 @@ class Coitem < ActiveRecord::Base 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) + if rentable.class == Video + return VideoPolicy.find_by_day(7).fee * days_late.to_i + else + return GamePolicy.find_by_day(7).fee * days_late.to_i + end + 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