Lots of stuff, I got too tired to keep perfect revision history
[cs356-p2-videostore.git] / app / models / video.rb
1 class Video < Rentable
2   has_many :video_genres
3   has_many :video_medias
4
5   validates_presence_of :director
6   validates_presence_of :video_genre_id
7   validates_presence_of :video_media_id
8
9   def genre
10     VideoGenre.find_by_id(video_genre_id)
11   end
12
13   def media
14     VideoMedia.find_by_id(video_media_id)
15   end
16
17   def calculated_price
18     day_of_week = Time.now.to_date.wday
19     base_fee = VideoPolicy.find_by_day(day_of_week).fee
20
21     # Check for newrelease
22     newrelease_fee = newrelease ? VideoPolicy.find_by_day(8).fee : 0.00
23
24     return base_fee + newrelease_fee
25   end
26
27   def due_date
28     # NOTE: a Date.wday will tell you the day of week (0-6, meaning Sunday-Saturday)
29     day_of_week = Time.now.to_date.wday
30     base_period = VideoPolicy.find_by_day(day_of_week).period
31     newrelease_period = newrelease ? VideoPolicy.find_by_day(8).period : 0
32
33     period = base_period + newrelease_period
34     return Time.now.advance(:days => period).to_date
35   end
36
37   protected
38   def validate
39     errors.add(:video_genre_id, "does not exist in the database") if genre.nil?
40     errors.add(:video_media_id, "does not exist in the database") if media.nil?
41   end
42 end