Add VideoPolicy to the purchase path
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 14:00:43 +0000 (06:00 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 14:00:43 +0000 (06:00 -0800)
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 <devel@irasnyder.com>
13 files changed:
app/controllers/video_policy_controller.rb [new file with mode: 0644]
app/helpers/video_policy_helper.rb [new file with mode: 0644]
app/models/coitem.rb
app/models/video.rb
app/models/video_policy.rb
app/views/layouts/video_policy.rhtml [new file with mode: 0644]
app/views/video_policy/_form.rhtml [new file with mode: 0644]
app/views/video_policy/edit.rhtml [new file with mode: 0644]
app/views/video_policy/list.rhtml [new file with mode: 0644]
app/views/video_policy/new.rhtml [new file with mode: 0644]
app/views/video_policy/show.rhtml [new file with mode: 0644]
db/development.sqlite3
test/functional/video_policy_controller_test.rb [new file with mode: 0644]

diff --git a/app/controllers/video_policy_controller.rb b/app/controllers/video_policy_controller.rb
new file mode 100644 (file)
index 0000000..4be73da
--- /dev/null
@@ -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 (file)
index 0000000..c9259c0
--- /dev/null
@@ -0,0 +1,2 @@
+module VideoPolicyHelper
+end
index 53bf312..b9d9024 100644 (file)
@@ -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
index 1f39114..52344e1 100644 (file)
@@ -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
index ec1e704..5e827bd 100644 (file)
@@ -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 (file)
index 0000000..416401e
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+  <title>VideoPolicy: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
diff --git a/app/views/video_policy/_form.rhtml b/app/views/video_policy/_form.rhtml
new file mode 100644 (file)
index 0000000..102b498
--- /dev/null
@@ -0,0 +1,16 @@
+<%= error_messages_for 'video_policy' %>
+
+<!--[form:video_policy]-->
+<p><label for="video_policy_day">Day</label><br/>
+<%= text_field 'video_policy', 'day'  %></p>
+
+<p><label for="video_policy_fee">Fee</label><br/>
+<%= text_field 'video_policy', 'fee'  %></p>
+
+<p><label for="video_policy_period">Period</label><br/>
+<%= text_field 'video_policy', 'period'  %></p>
+
+<p><label for="video_policy_description">Description</label><br/>
+<%= text_field 'video_policy', 'description'  %></p>
+<!--[eoform:video_policy]-->
+
diff --git a/app/views/video_policy/edit.rhtml b/app/views/video_policy/edit.rhtml
new file mode 100644 (file)
index 0000000..cdcb61b
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing video_policy</h1>
+
+<% 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 (file)
index 0000000..2b916c6
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing video_policies</h1>
+
+<table>
+  <tr>
+  <% for column in VideoPolicy.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for video_policy in @video_policies %>
+  <tr>
+  <% for column in VideoPolicy.content_columns %>
+    <td><%=h video_policy.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => video_policy %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => video_policy %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => video_policy }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= 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 %> 
+
+<br />
+
+<%= 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 (file)
index 0000000..3df6e40
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New video_policy</h1>
+
+<% 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 (file)
index 0000000..f6c9c33
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in VideoPolicy.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @video_policy.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @video_policy %> |
+<%= link_to 'Back', :action => 'list' %>
index 41a0f85..2b880e6 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/test/functional/video_policy_controller_test.rb b/test/functional/video_policy_controller_test.rb
new file mode 100644 (file)
index 0000000..57b1550
--- /dev/null
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'video_policy_controller'
+
+# Re-raise errors caught by the controller.
+class VideoPolicyController; def rescue_action(e) raise e end; end
+
+class VideoPolicyControllerTest < Test::Unit::TestCase
+  fixtures :video_policies
+
+  def setup
+    @controller = VideoPolicyController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = video_policies(:first).id
+  end
+
+  def test_index
+    get :index
+    assert_response :success
+    assert_template 'list'
+  end
+
+  def test_list
+    get :list
+
+    assert_response :success
+    assert_template 'list'
+
+    assert_not_nil assigns(:video_policies)
+  end
+
+  def test_show
+    get :show, :id => @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