X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fmodels%2Fcustomer.rb;h=acd686900f47ad7af523c214bee264541a460e41;hb=d9ccb8cf52aee119343545cb734e185580daf352;hp=1a86679c408058700a2bcbd098ee8c21f92312d7;hpb=be6d865110840adfc90f572a4cf14f62bc4cf31e;p=cs356-p2-videostore.git diff --git a/app/models/customer.rb b/app/models/customer.rb index 1a86679..acd6869 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,13 +1,69 @@ class Customer < ActiveRecord::Base - has_many :coitem + has_many :coitems + has_many :merchandise_purchases + has_many :rentable_purchases validates_presence_of :name, :email, :phone, :address validates_numericality_of :debt + def checked_out_videos + coitems = Coitem.find_all_by_customer_id(id) + video_count = 0 + + for item in coitems + if item.rentable.class == Video + video_count += 1 + end + end + + return video_count + end + + def checked_out_games + coitems = Coitem.find_all_by_customer_id(id) + game_count = 0 + + for item in coitems + if item.rentable.class == Game + game_count += 1 + end + end + + return game_count + end + + def overdue_videos + coitems = Coitem.find_all_by_customer_id(id) + overdue_video_count = 0 + + for item in coitems + if item.rentable.class == Video and item.overdue? + overdue_video_count += 1 + end + end + + return overdue_video_count + end + + def overdue_games + coitems = Coitem.find_all_by_customer_id(id) + overdue_game_count = 0 + + for item in coitems + if item.rentable.class == Game and item.overdue? + overdue_game_count += 1 + end + end + + return overdue_game_count + end + protected def validate errors.add(:debt, "should be non-negative") if debt.nil? || debt < 0.00 + errors.add(:email, "is invalid") unless email =~ /.+@.+\..+/ + errors.add(:phone, "has invalid format, use XXX-XXX-XXXX") unless phone =~ /^\d{3}-\d{3}-\d{4}$/ end end