X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fmodels%2Fpurchase.rb;h=26cf518a7458ff2019fbfe3cdbf1075c487d2594;hb=53634274872ef0ba36cbf94e23df4443ea5613d5;hp=023f423a730ee39a377f17511ac0b9cce19f3cf2;hpb=97d39268c0b348898dca1ec24552eb26a94ae546;p=cs356-p2-videostore.git diff --git a/app/models/purchase.rb b/app/models/purchase.rb index 023f423..26cf518 100644 --- a/app/models/purchase.rb +++ b/app/models/purchase.rb @@ -1,4 +1,23 @@ class Purchase < ActiveRecord::Base belongs_to :customer - belongs_to :purchaseable, :polymorphic => true + + validates_presence_of :customer_id + validates_presence_of :date + validates_presence_of :price + validates_numericality_of :price + + def title + if type == MerchandisePurchase + return merchandise.title + else + return rentable.title + end + end + + protected + def validate + errors.add(:price, "cannot be negative") if price < 0 + errors.add(:price, "cannot be less than $0.01") if price < 0.01 + errors.add(:customer_id, "does not exist in the database") if customer.nil? + end end