Add GamePolicy and integrate with the Game model
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 18:55:33 +0000 (10:55 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 18:55:33 +0000 (10:55 -0800)
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 <devel@irasnyder.com>
16 files changed:
app/controllers/game_policy_controller.rb [new file with mode: 0644]
app/helpers/game_policy_helper.rb [new file with mode: 0644]
app/models/game.rb
app/models/game_policy.rb [new file with mode: 0644]
app/views/game_policy/_form.rhtml [new file with mode: 0644]
app/views/game_policy/edit.rhtml [new file with mode: 0644]
app/views/game_policy/list.rhtml [new file with mode: 0644]
app/views/game_policy/new.rhtml [new file with mode: 0644]
app/views/game_policy/show.rhtml [new file with mode: 0644]
app/views/layouts/game_policy.rhtml [new file with mode: 0644]
db/development.sqlite3
db/migrate/023_create_game_policies.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/game_policies.yml [new file with mode: 0644]
test/functional/game_policy_controller_test.rb [new file with mode: 0644]
test/unit/game_policy_test.rb [new file with mode: 0644]

diff --git a/app/controllers/game_policy_controller.rb b/app/controllers/game_policy_controller.rb
new file mode 100644 (file)
index 0000000..9ef608f
--- /dev/null
@@ -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 (file)
index 0000000..6cd52fe
--- /dev/null
@@ -0,0 +1,2 @@
+module GamePolicyHelper
+end
index bd1f902..6c6b59b 100644 (file)
@@ -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 (file)
index 0000000..12eb47b
--- /dev/null
@@ -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 (file)
index 0000000..8687fed
--- /dev/null
@@ -0,0 +1,16 @@
+<%= error_messages_for 'game_policy' %>
+
+<!--[form:game_policy]-->
+<p><label for="game_policy_day">Day</label><br/>
+<%= text_field 'game_policy', 'day'  %></p>
+
+<p><label for="game_policy_fee">Fee</label><br/>
+<%= text_field 'game_policy', 'fee'  %></p>
+
+<p><label for="game_policy_period">Period</label><br/>
+<%= text_field 'game_policy', 'period'  %></p>
+
+<p><label for="game_policy_description">Description</label><br/>
+<%= text_field 'game_policy', 'description'  %></p>
+<!--[eoform:game_policy]-->
+
diff --git a/app/views/game_policy/edit.rhtml b/app/views/game_policy/edit.rhtml
new file mode 100644 (file)
index 0000000..ad3a557
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing game_policy</h1>
+
+<% 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 (file)
index 0000000..761de30
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing game_policies</h1>
+
+<table>
+  <tr>
+  <% for column in GamePolicy.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for game_policy in @game_policies %>
+  <tr>
+  <% for column in GamePolicy.content_columns %>
+    <td><%=h game_policy.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => game_policy %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => game_policy %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => game_policy }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= 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 %> 
+
+<br />
+
+<%= 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 (file)
index 0000000..90ca199
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New game_policy</h1>
+
+<% 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 (file)
index 0000000..419e495
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in GamePolicy.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @game_policy.send(column.name) %>
+</p>
+<% 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 (file)
index 0000000..07b0445
--- /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>GamePolicy: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
index 2b880e6..3c5a59d 100644 (file)
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 (file)
index 0000000..e4a1dfe
--- /dev/null
@@ -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
index c89ecac..5e0c608 100644 (file)
@@ -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 (file)
index 0000000..b49c4eb
--- /dev/null
@@ -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 (file)
index 0000000..5d95747
--- /dev/null
@@ -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 (file)
index 0000000..bccc4a7
--- /dev/null
@@ -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