From: Ira W. Snyder Date: Fri, 23 Nov 2007 18:55:33 +0000 (-0800) Subject: Add GamePolicy and integrate with the Game model X-Git-Tag: turned-in~48 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=bf5c679032a167a9eeb4aa16284cd5dd2426a12a;p=cs356-p2-videostore.git Add GamePolicy and integrate with the Game model This makes things like rental periods based on day of the week come from the database. It also adds correct calculation of late fees. Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/game_policy_controller.rb b/app/controllers/game_policy_controller.rb new file mode 100644 index 0000000..9ef608f --- /dev/null +++ b/app/controllers/game_policy_controller.rb @@ -0,0 +1,51 @@ +class GamePolicyController < 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 + @game_policy_pages, @game_policies = paginate :game_policies, :per_page => 10 + end + + def show + @game_policy = GamePolicy.find(params[:id]) + end + + def new + @game_policy = GamePolicy.new + end + + def create + @game_policy = GamePolicy.new(params[:game_policy]) + if @game_policy.save + flash[:notice] = 'GamePolicy was successfully created.' + redirect_to :action => 'list' + else + render :action => 'new' + end + end + + def edit + @game_policy = GamePolicy.find(params[:id]) + end + + def update + @game_policy = GamePolicy.find(params[:id]) + if @game_policy.update_attributes(params[:game_policy]) + flash[:notice] = 'GamePolicy was successfully updated.' + redirect_to :action => 'show', :id => @game_policy + else + render :action => 'edit' + end + end + + def destroy + GamePolicy.find(params[:id]).destroy + redirect_to :action => 'list' + end +end diff --git a/app/helpers/game_policy_helper.rb b/app/helpers/game_policy_helper.rb new file mode 100644 index 0000000..6cd52fe --- /dev/null +++ b/app/helpers/game_policy_helper.rb @@ -0,0 +1,2 @@ +module GamePolicyHelper +end diff --git a/app/models/game.rb b/app/models/game.rb index bd1f902..6c6b59b 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -3,14 +3,25 @@ class Game < Rentable validates_presence_of :platform 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 = GamePolicy.find_by_day(day_of_week).fee + + # Check for newrelease + newrelease_fee = newrelease ? GamePolicy.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 = GamePolicy.find_by_day(day_of_week).period + newrelease_period = newrelease ? GamePolicy.find_by_day(8).period : 0 + + period = base_period + newrelease_period + return Time.now.advance(:days => period).to_date end end diff --git a/app/models/game_policy.rb b/app/models/game_policy.rb new file mode 100644 index 0000000..12eb47b --- /dev/null +++ b/app/models/game_policy.rb @@ -0,0 +1,15 @@ +class GamePolicy < 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 + + protected + def validate + errors.add(:fee, "must be greater than $0.01") if fee < 0.01 + end +end diff --git a/app/views/game_policy/_form.rhtml b/app/views/game_policy/_form.rhtml new file mode 100644 index 0000000..8687fed --- /dev/null +++ b/app/views/game_policy/_form.rhtml @@ -0,0 +1,16 @@ +<%= error_messages_for 'game_policy' %> + + +


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

+ +


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

+ +


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

+ +


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

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

Editing game_policy

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

Listing game_policies

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

New game_policy

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

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

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @game_policy %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/layouts/game_policy.rhtml b/app/views/layouts/game_policy.rhtml new file mode 100644 index 0000000..07b0445 --- /dev/null +++ b/app/views/layouts/game_policy.rhtml @@ -0,0 +1,17 @@ + + + + + + GamePolicy: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 2b880e6..3c5a59d 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ diff --git a/db/migrate/023_create_game_policies.rb b/db/migrate/023_create_game_policies.rb new file mode 100644 index 0000000..e4a1dfe --- /dev/null +++ b/db/migrate/023_create_game_policies.rb @@ -0,0 +1,14 @@ +class CreateGamePolicies < ActiveRecord::Migration + def self.up + create_table :game_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 :game_policies + end +end diff --git a/db/schema.rb b/db/schema.rb index c89ecac..5e0c608 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 => 22) do +ActiveRecord::Schema.define(:version => 23) do create_table "bitems", :force => true do |t| t.column "customer_id", :integer, :null => false @@ -25,6 +25,13 @@ ActiveRecord::Schema.define(:version => 22) do t.column "debt", :decimal, :default => 0.0 end + create_table "game_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 "medias", :force => true do |t| t.column "name", :string, :null => false end diff --git a/test/fixtures/game_policies.yml b/test/fixtures/game_policies.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/game_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/functional/game_policy_controller_test.rb b/test/functional/game_policy_controller_test.rb new file mode 100644 index 0000000..5d95747 --- /dev/null +++ b/test/functional/game_policy_controller_test.rb @@ -0,0 +1,92 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'game_policy_controller' + +# Re-raise errors caught by the controller. +class GamePolicyController; def rescue_action(e) raise e end; end + +class GamePolicyControllerTest < Test::Unit::TestCase + fixtures :game_policies + + def setup + @controller = GamePolicyController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @first_id = game_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(:game_policies) + end + + def test_show + get :show, :id => @first_id + + assert_response :success + assert_template 'show' + + assert_not_nil assigns(:game_policy) + assert assigns(:game_policy).valid? + end + + def test_new + get :new + + assert_response :success + assert_template 'new' + + assert_not_nil assigns(:game_policy) + end + + def test_create + num_game_policies = GamePolicy.count + + post :create, :game_policy => {} + + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_equal num_game_policies + 1, GamePolicy.count + end + + def test_edit + get :edit, :id => @first_id + + assert_response :success + assert_template 'edit' + + assert_not_nil assigns(:game_policy) + assert assigns(:game_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 { + GamePolicy.find(@first_id) + } + + post :destroy, :id => @first_id + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_raise(ActiveRecord::RecordNotFound) { + GamePolicy.find(@first_id) + } + end +end diff --git a/test/unit/game_policy_test.rb b/test/unit/game_policy_test.rb new file mode 100644 index 0000000..bccc4a7 --- /dev/null +++ b/test/unit/game_policy_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class GamePolicyTest < Test::Unit::TestCase + fixtures :game_policies + + # Replace this with your real tests. + def test_truth + assert true + end +end