From 01114330b962728805d3c43f2f1a1423f2bd66b3 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Fri, 23 Nov 2007 06:00:43 -0800 Subject: [PATCH] Add VideoPolicy to the purchase path This makes video checkout / return really charge the appropriate fees based on day of the week and newrelease status. It also makes late fees be charged at the proper rate. Signed-off-by: Ira W. Snyder --- app/controllers/video_policy_controller.rb | 51 ++++++++++ app/helpers/video_policy_helper.rb | 2 + app/models/coitem.rb | 3 +- app/models/video.rb | 17 +++- app/models/video_policy.rb | 20 ---- app/views/layouts/video_policy.rhtml | 17 ++++ app/views/video_policy/_form.rhtml | 16 +++ app/views/video_policy/edit.rhtml | 9 ++ app/views/video_policy/list.rhtml | 27 +++++ app/views/video_policy/new.rhtml | 8 ++ app/views/video_policy/show.rhtml | 8 ++ db/development.sqlite3 | Bin 20480 -> 20480 bytes .../video_policy_controller_test.rb | 92 ++++++++++++++++++ 13 files changed, 245 insertions(+), 25 deletions(-) create mode 100644 app/controllers/video_policy_controller.rb create mode 100644 app/helpers/video_policy_helper.rb create mode 100644 app/views/layouts/video_policy.rhtml create mode 100644 app/views/video_policy/_form.rhtml create mode 100644 app/views/video_policy/edit.rhtml create mode 100644 app/views/video_policy/list.rhtml create mode 100644 app/views/video_policy/new.rhtml create mode 100644 app/views/video_policy/show.rhtml create mode 100644 test/functional/video_policy_controller_test.rb diff --git a/app/controllers/video_policy_controller.rb b/app/controllers/video_policy_controller.rb new file mode 100644 index 0000000..4be73da --- /dev/null +++ b/app/controllers/video_policy_controller.rb @@ -0,0 +1,51 @@ +class VideoPolicyController < ApplicationController + def index + list + render :action => 'list' + end + + # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) + verify :method => :post, :only => [ :destroy, :create, :update ], + :redirect_to => { :action => :list } + + def list + @video_policy_pages, @video_policies = paginate :video_policies, :per_page => 10 + end + + def show + @video_policy = VideoPolicy.find(params[:id]) + end + + def new + @video_policy = VideoPolicy.new + end + + def create + @video_policy = VideoPolicy.new(params[:video_policy]) + if @video_policy.save + flash[:notice] = 'VideoPolicy was successfully created.' + redirect_to :action => 'list' + else + render :action => 'new' + end + end + + def edit + @video_policy = VideoPolicy.find(params[:id]) + end + + def update + @video_policy = VideoPolicy.find(params[:id]) + if @video_policy.update_attributes(params[:video_policy]) + flash[:notice] = 'VideoPolicy was successfully updated.' + redirect_to :action => 'show', :id => @video_policy + else + render :action => 'edit' + end + end + + def destroy + VideoPolicy.find(params[:id]).destroy + redirect_to :action => 'list' + end +end diff --git a/app/helpers/video_policy_helper.rb b/app/helpers/video_policy_helper.rb new file mode 100644 index 0000000..c9259c0 --- /dev/null +++ b/app/helpers/video_policy_helper.rb @@ -0,0 +1,2 @@ +module VideoPolicyHelper +end diff --git a/app/models/coitem.rb b/app/models/coitem.rb index 53bf312..b9d9024 100644 --- a/app/models/coitem.rb +++ b/app/models/coitem.rb @@ -22,9 +22,8 @@ class Coitem < ActiveRecord::Base end def late_fee - # FIXME: this should be calculated better days_late = Time.now.to_date - (due_date) - return 3 * days_late.to_i + return VideoPolicy.find_by_day(8).fee * days_late.to_i end protected diff --git a/app/models/video.rb b/app/models/video.rb index 1f39114..52344e1 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -7,14 +7,25 @@ class Video < Rentable validates_presence_of :media def calculated_price - # FIXME: generate this based on day of week, newrelase - return 11 + # 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) - return Time.now.advance(:days => 2).to_date + 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 diff --git a/app/models/video_policy.rb b/app/models/video_policy.rb index ec1e704..5e827bd 100644 --- a/app/models/video_policy.rb +++ b/app/models/video_policy.rb @@ -8,26 +8,6 @@ class VideoPolicy < ActiveRecord::Base 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 diff --git a/app/views/layouts/video_policy.rhtml b/app/views/layouts/video_policy.rhtml new file mode 100644 index 0000000..416401e --- /dev/null +++ b/app/views/layouts/video_policy.rhtml @@ -0,0 +1,17 @@ + + + + + + VideoPolicy: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/video_policy/_form.rhtml b/app/views/video_policy/_form.rhtml new file mode 100644 index 0000000..102b498 --- /dev/null +++ b/app/views/video_policy/_form.rhtml @@ -0,0 +1,16 @@ +<%= error_messages_for 'video_policy' %> + + +


+<%= text_field 'video_policy', 'day' %>

+ +


+<%= text_field 'video_policy', 'fee' %>

+ +


+<%= text_field 'video_policy', 'period' %>

+ +


+<%= text_field 'video_policy', 'description' %>

+ + diff --git a/app/views/video_policy/edit.rhtml b/app/views/video_policy/edit.rhtml new file mode 100644 index 0000000..cdcb61b --- /dev/null +++ b/app/views/video_policy/edit.rhtml @@ -0,0 +1,9 @@ +

Editing video_policy

+ +<% form_tag :action => 'update', :id => @video_policy do %> + <%= render :partial => 'form' %> + <%= submit_tag 'Edit' %> +<% end %> + +<%= link_to 'Show', :action => 'show', :id => @video_policy %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/video_policy/list.rhtml b/app/views/video_policy/list.rhtml new file mode 100644 index 0000000..2b916c6 --- /dev/null +++ b/app/views/video_policy/list.rhtml @@ -0,0 +1,27 @@ +

Listing video_policies

+ + + + <% for column in VideoPolicy.content_columns %> + + <% end %> + + +<% for video_policy in @video_policies %> + + <% for column in VideoPolicy.content_columns %> + + <% end %> + + + + +<% end %> +
<%= column.human_name %>
<%=h video_policy.send(column.name) %><%= link_to 'Show', :action => 'show', :id => video_policy %><%= link_to 'Edit', :action => 'edit', :id => video_policy %><%= link_to 'Destroy', { :action => 'destroy', :id => video_policy }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= link_to 'Previous page', { :page => @video_policy_pages.current.previous } if @video_policy_pages.current.previous %> +<%= link_to 'Next page', { :page => @video_policy_pages.current.next } if @video_policy_pages.current.next %> + +
+ +<%= link_to 'New video_policy', :action => 'new' %> diff --git a/app/views/video_policy/new.rhtml b/app/views/video_policy/new.rhtml new file mode 100644 index 0000000..3df6e40 --- /dev/null +++ b/app/views/video_policy/new.rhtml @@ -0,0 +1,8 @@ +

New video_policy

+ +<% form_tag :action => 'create' do %> + <%= render :partial => 'form' %> + <%= submit_tag "Create" %> +<% end %> + +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/video_policy/show.rhtml b/app/views/video_policy/show.rhtml new file mode 100644 index 0000000..f6c9c33 --- /dev/null +++ b/app/views/video_policy/show.rhtml @@ -0,0 +1,8 @@ +<% for column in VideoPolicy.content_columns %> +

+ <%= column.human_name %>: <%=h @video_policy.send(column.name) %> +

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @video_policy %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 41a0f85c48f3f7f4edbf2def6015e1266a22d69b..2b880e6f5f936f14177a643299b97f2233aca9ec 100644 GIT binary patch delta 663 zcmZozz}T>Wae_4Cjg2xdxH*`Zk1#MlVm`82kYxummkbjd10$oNA|vPI4g4Y&GK^3z zlaYaexvrt1u8}dEWyZ_Ez`)5ghk^Mi(9O5##2k!ty4}0*nol*{2~Rv)N+NO)ST4B;?!a} z4zPN2c2-6wAYZ{Ju_P6&2dG}bC9%?w9V{Qq#>@cQVP+b4^9@0k&Fa&_~z#U-DOm)4^BCZkqiWN1ebzz6axU5#Ir^K delta 343 zcmZozz}T>Wae_4Cv5hh>xCIzk7#Ntdn0;VyW1|IgJ#z3utDbpwBWz1bnr7wgQ @first_id + + assert_response :success + assert_template 'show' + + assert_not_nil assigns(:video_policy) + assert assigns(:video_policy).valid? + end + + def test_new + get :new + + assert_response :success + assert_template 'new' + + assert_not_nil assigns(:video_policy) + end + + def test_create + num_video_policies = VideoPolicy.count + + post :create, :video_policy => {} + + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_equal num_video_policies + 1, VideoPolicy.count + end + + def test_edit + get :edit, :id => @first_id + + assert_response :success + assert_template 'edit' + + assert_not_nil assigns(:video_policy) + assert assigns(:video_policy).valid? + end + + def test_update + post :update, :id => @first_id + assert_response :redirect + assert_redirected_to :action => 'show', :id => @first_id + end + + def test_destroy + assert_nothing_raised { + VideoPolicy.find(@first_id) + } + + post :destroy, :id => @first_id + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_raise(ActiveRecord::RecordNotFound) { + VideoPolicy.find(@first_id) + } + end +end -- 2.25.1