From 09ddd1ef5546ec679c832c7cefb93f00125c96c6 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Fri, 23 Nov 2007 02:38:06 -0800 Subject: [PATCH] Add the VideoPolicy model, to hold all the base prices and periods Signed-off-by: Ira W. Snyder --- app/models/video_policy.rb | 35 ++++++++++++++++++++++++ db/development.sqlite3 | Bin 19456 -> 20480 bytes db/migrate/022_create_video_policies.rb | 14 ++++++++++ db/schema.rb | 9 +++++- test/fixtures/video_policies.yml | 5 ++++ test/unit/video_policy_test.rb | 10 +++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/models/video_policy.rb create mode 100644 db/migrate/022_create_video_policies.rb create mode 100644 test/fixtures/video_policies.yml create mode 100644 test/unit/video_policy_test.rb diff --git a/app/models/video_policy.rb b/app/models/video_policy.rb new file mode 100644 index 0000000..ec1e704 --- /dev/null +++ b/app/models/video_policy.rb @@ -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 diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 72946714465818e87ab8b87eb62d19b6df64b970..41a0f85c48f3f7f4edbf2def6015e1266a22d69b 100644 GIT binary patch delta 311 zcmZpe!Pu~Xae}nqF$PrNv{C0WJEPbpjtpi-p~;!tvjrGf7#Ntdn0;VyW1|H#3v&m9 z+~fd$DWDv~w8?o8M#bbF2qS9pDt;*qpp*?GqoN`slaYaexvrt1uAv!>Wn=)9l>^H% zPTs&TvPpnZfgdQu!eGq6w43n*qcnr@L`P-2##AA8aZOFeX4aC#q@2{U%#_sp_=5bL z%;e0}VpN`pbC9cJh^s<~qmz%T0$6VHbYp$yl*G!(_mxDI(^6BF6jD-?GjkJjG%R$C zG!^{(Llpc%eSCBjlnPRdGV@a=7br?imNS%`{6UO|QFC&(!PUvFMyEGB7`iepGLcwh G!U6z3bxl$L delta 201 zcmZozz}PT@ae}nqP6kw9zftEhJEQ0(jtpi-&dHhFvjy0geHfS@F&|-G#@xZ2#q6`O z(Sn&phK-G3asa;+iwrA}kq2V102w_X1~ZVcieE}YhKY@Vkx@~Rk;%xwz+BhRP}k55 z#xgRHVT8&uPTs&TvPpnZftNXeftewjfzg4fl_7g#W9(#U!>gNj8@V!0{$O~T3+T=b V;`;iGO`MZe4HvNhxh#uJBmhGvC}scv diff --git a/db/migrate/022_create_video_policies.rb b/db/migrate/022_create_video_policies.rb new file mode 100644 index 0000000..0a44934 --- /dev/null +++ b/db/migrate/022_create_video_policies.rb @@ -0,0 +1,14 @@ +class CreateVideoPolicies < ActiveRecord::Migration + def self.up + create_table :video_policies do |t| + t.column :day, :integer, :null => false + t.column :fee, :decimal, :precision => 8, :scale => 2, :null => false + t.column :period, :integer + t.column :description, :string + end + end + + def self.down + drop_table :video_policies + end +end diff --git a/db/schema.rb b/db/schema.rb index 8132da3..c89ecac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,7 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 21) do +ActiveRecord::Schema.define(:version => 22) do create_table "bitems", :force => true do |t| t.column "customer_id", :integer, :null => false @@ -68,6 +68,13 @@ ActiveRecord::Schema.define(:version => 21) do # Could not dump table "sqlite_sequence" because of following StandardError # Unknown type '' for column 'name' + create_table "video_policies", :force => true do |t| + t.column "day", :integer, :null => false + t.column "fee", :decimal, :precision => 8, :scale => 2, :null => false + t.column "period", :integer + t.column "description", :string + end + create_table "videogenres", :force => true do |t| t.column "name", :string, :null => false end diff --git a/test/fixtures/video_policies.yml b/test/fixtures/video_policies.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/video_policies.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 +two: + id: 2 diff --git a/test/unit/video_policy_test.rb b/test/unit/video_policy_test.rb new file mode 100644 index 0000000..a41340d --- /dev/null +++ b/test/unit/video_policy_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class VideoPolicyTest < Test::Unit::TestCase + fixtures :video_policies + + # Replace this with your real tests. + def test_truth + assert true + end +end -- 2.25.1