Add GamePolicy and integrate with the Game model
[cs356-p2-videostore.git] / app / models / video.rb
index 50733d5..52344e1 100644 (file)
@@ -1,5 +1,36 @@
 class Video < Rentable
+  has_many :video_genres
+  has_many :medias
+
   validates_presence_of :director
   validates_presence_of :video_genre
   validates_presence_of :media
+
+  def calculated_price
+    # FIXME: generate this based on day of week, newrelease
+    day_of_week = Time.now.to_date.wday
+    base_fee = VideoPolicy.find_by_day(day_of_week).fee
+
+    # Check for newrelease
+    newrelease_fee = newrelease ? VideoPolicy.find_by_day(8).fee : 0.00
+
+    return base_fee + newrelease_fee
+  end
+
+  def due_date
+    # FIXME: generate this based on the day of week, newrelease
+    # NOTE: a Date.wday will tell you the day of week (0-6, meaning Sunday-Saturday)
+    day_of_week = Time.now.to_date.wday
+    base_period = VideoPolicy.find_by_day(day_of_week).period
+    newrelease_period = newrelease ? VideoPolicy.find_by_day(8).period : 0
+
+    period = base_period + newrelease_period
+    return Time.now.advance(:days => period).to_date
+  end
+
+  protected
+  def validate
+    errors.add(:video_genre, "does not exist in the database") if video_genre.nil?
+    errors.add(:media, "does not exist in the database") if media.nil?
+  end
 end