--- /dev/null
+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
--- /dev/null
+module VideoPolicyHelper
+end
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
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
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
--- /dev/null
+<!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>
--- /dev/null
+<%= 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]-->
+
--- /dev/null
+<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' %>
--- /dev/null
+<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' %>
--- /dev/null
+<h1>New video_policy</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% 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' %>
--- /dev/null
+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