Add the VideoPolicy model, to hold all the base prices and periods
[cs356-p2-videostore.git] / app / models / video_policy.rb
diff --git a/app/models/video_policy.rb b/app/models/video_policy.rb
new file mode 100644 (file)
index 0000000..ec1e704
--- /dev/null
@@ -0,0 +1,35 @@
+class VideoPolicy < ActiveRecord::Base
+  validates_presence_of :day
+  validates_presence_of :fee
+  validates_presence_of :period
+  validates_presence_of :description
+
+  validates_numericality_of :day
+  validates_numericality_of :fee
+  validates_numericality_of :period
+
+  # Find the base fee for today
+  def todays_fee
+    # Gets the current day of the week in 0-6 == Sun-Sat form
+    day_of_week = Time.now.to_date.wday
+    return VideoPolicy.find_by_day(day_of_week).fee
+  end
+
+  # Find the base rental period for today
+  def todays_period
+    # Gets the current day of the week in 0-6 == Sun-Sat form
+    day_of_week = Time.now.to_date.wday
+    return VideoPolicy.find_by_day(day_of_week).period
+  end
+
+  # Find the fee for overdue videos (per day)
+  def overdue_fee
+    overdue_day = 7
+    return VideoPolicy.find_by_day(overdue_day).fee
+  end
+
+  protected
+  def validate
+    errors.add(:fee, "must be greater than $0.01") if fee < 0.01
+  end
+end