Massive Cleanup
[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     # FIXME: generate this based on day of week, newrelease
19     day_of_week = Time.now.to_date.wday
20     base_fee = VideoPolicy.find_by_day(day_of_week).fee
21
22     # Check for newrelease
23     newrelease_fee = newrelease ? VideoPolicy.find_by_day(8).fee : 0.00
24
25     return base_fee + newrelease_fee
26   end
27
28   def due_date
29     # FIXME: generate this based on the day of week, newrelease
30     # NOTE: a Date.wday will tell you the day of week (0-6, meaning Sunday-Saturday)
31     day_of_week = Time.now.to_date.wday
32     base_period = VideoPolicy.find_by_day(day_of_week).period
33     newrelease_period = newrelease ? VideoPolicy.find_by_day(8).period : 0
34
35     period = base_period + newrelease_period
36     return Time.now.advance(:days => period).to_date
37   end
38
39   protected
40   def validate
41     errors.add(:video_genre, "does not exist in the database") if video_genre.nil?
42     errors.add(:video_media, "does not exist in the database") if video_media.nil?
43   end
44 end