Add RentablePolicy, to get set up for adding policies
authorIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:50:16 +0000 (00:50 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Fri, 23 Nov 2007 08:50:16 +0000 (00:50 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
17 files changed:
app/controllers/rentable_policy_controller.rb [new file with mode: 0644]
app/helpers/rentable_policy_helper.rb [new file with mode: 0644]
app/models/game.rb
app/models/rentable_policy.rb [new file with mode: 0644]
app/models/video.rb
app/views/layouts/rentable_policy.rhtml [new file with mode: 0644]
app/views/rentable_policy/_form.rhtml [new file with mode: 0644]
app/views/rentable_policy/edit.rhtml [new file with mode: 0644]
app/views/rentable_policy/list.rhtml [new file with mode: 0644]
app/views/rentable_policy/new.rhtml [new file with mode: 0644]
app/views/rentable_policy/show.rhtml [new file with mode: 0644]
db/development.sqlite3
db/migrate/021_create_rentable_policies.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/rentable_policies.yml [new file with mode: 0644]
test/functional/rentable_policy_controller_test.rb [new file with mode: 0644]
test/unit/rentable_policy_test.rb [new file with mode: 0644]

diff --git a/app/controllers/rentable_policy_controller.rb b/app/controllers/rentable_policy_controller.rb
new file mode 100644 (file)
index 0000000..f99fbed
--- /dev/null
@@ -0,0 +1,51 @@
+class RentablePolicyController < 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
+    @rentable_policy_pages, @rentable_policies = paginate :rentable_policies, :per_page => 10
+  end
+
+  def show
+    @rentable_policy = RentablePolicy.find(params[:id])
+  end
+
+  def new
+    @rentable_policy = RentablePolicy.new
+  end
+
+  def create
+    @rentable_policy = RentablePolicy.new(params[:rentable_policy])
+    if @rentable_policy.save
+      flash[:notice] = 'RentablePolicy was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @rentable_policy = RentablePolicy.find(params[:id])
+  end
+
+  def update
+    @rentable_policy = RentablePolicy.find(params[:id])
+    if @rentable_policy.update_attributes(params[:rentable_policy])
+      flash[:notice] = 'RentablePolicy was successfully updated.'
+      redirect_to :action => 'show', :id => @rentable_policy
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    RentablePolicy.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end
diff --git a/app/helpers/rentable_policy_helper.rb b/app/helpers/rentable_policy_helper.rb
new file mode 100644 (file)
index 0000000..cbbd668
--- /dev/null
@@ -0,0 +1,2 @@
+module RentablePolicyHelper
+end
index 872b45a..bd1f902 100644 (file)
@@ -9,6 +9,7 @@ class Game < Rentable
 
   def due_date
     # FIXME: generate this based on the day of week, newrelease
 
   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
   end
 
     return Time.now.advance(:days => 2).to_date
   end
 
diff --git a/app/models/rentable_policy.rb b/app/models/rentable_policy.rb
new file mode 100644 (file)
index 0000000..610f13e
--- /dev/null
@@ -0,0 +1,2 @@
+class RentablePolicy < ActiveRecord::Base
+end
index 6f75a26..1f39114 100644 (file)
@@ -13,6 +13,7 @@ class Video < Rentable
 
   def due_date
     # FIXME: generate this based on the day of week, newrelease
 
   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
   end
 
     return Time.now.advance(:days => 2).to_date
   end
 
diff --git a/app/views/layouts/rentable_policy.rhtml b/app/views/layouts/rentable_policy.rhtml
new file mode 100644 (file)
index 0000000..ac13d73
--- /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>RentablePolicy: <%= 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/rentable_policy/_form.rhtml b/app/views/rentable_policy/_form.rhtml
new file mode 100644 (file)
index 0000000..ad40e56
--- /dev/null
@@ -0,0 +1,13 @@
+<%= error_messages_for 'rentable_policy' %>
+
+<!--[form:rentable_policy]-->
+<p><label for="rentable_policy_name">Name</label><br/>
+<%= text_field 'rentable_policy', 'name'  %></p>
+
+<p><label for="rentable_policy_value">Value</label><br/>
+<%= text_field 'rentable_policy', 'value'  %></p>
+
+<p><label for="rentable_policy_description">Description</label><br/>
+<%= text_field 'rentable_policy', 'description' %></p>
+<!--[eoform:rentable_policy]-->
+
diff --git a/app/views/rentable_policy/edit.rhtml b/app/views/rentable_policy/edit.rhtml
new file mode 100644 (file)
index 0000000..cff2755
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing rentable_policy</h1>
+
+<% form_tag :action => 'update', :id => @rentable_policy do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @rentable_policy %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/rentable_policy/list.rhtml b/app/views/rentable_policy/list.rhtml
new file mode 100644 (file)
index 0000000..b5a4833
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing rentable_policies</h1>
+
+<table>
+  <tr>
+  <% for column in RentablePolicy.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for rentable_policy in @rentable_policies %>
+  <tr>
+  <% for column in RentablePolicy.content_columns %>
+    <td><%=h rentable_policy.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => rentable_policy %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => rentable_policy %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => rentable_policy }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @rentable_policy_pages.current.previous } if @rentable_policy_pages.current.previous %>
+<%= link_to 'Next page', { :page => @rentable_policy_pages.current.next } if @rentable_policy_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New rentable_policy', :action => 'new' %>
diff --git a/app/views/rentable_policy/new.rhtml b/app/views/rentable_policy/new.rhtml
new file mode 100644 (file)
index 0000000..1470ff8
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New rentable_policy</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/rentable_policy/show.rhtml b/app/views/rentable_policy/show.rhtml
new file mode 100644 (file)
index 0000000..273bd34
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in RentablePolicy.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @rentable_policy.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @rentable_policy %> |
+<%= link_to 'Back', :action => 'list' %>
index d7f8a65..f7ee02e 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/021_create_rentable_policies.rb b/db/migrate/021_create_rentable_policies.rb
new file mode 100644 (file)
index 0000000..67adbe5
--- /dev/null
@@ -0,0 +1,13 @@
+class CreateRentablePolicies < ActiveRecord::Migration
+  def self.up
+    create_table :rentable_policies do |t|
+      t.column :name, :string, :null => false
+      t.column :value, :integer
+      t.column :description, :string, :null => false
+    end
+  end
+
+  def self.down
+    drop_table :rentable_policies
+  end
+end
index ae1459b..8132da3 100644 (file)
@@ -2,7 +2,7 @@
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
-ActiveRecord::Schema.define(:version => 20) do
+ActiveRecord::Schema.define(:version => 21) do
 
   create_table "bitems", :force => true do |t|
     t.column "customer_id",    :integer, :null => false
 
   create_table "bitems", :force => true do |t|
     t.column "customer_id",    :integer, :null => false
@@ -48,6 +48,12 @@ ActiveRecord::Schema.define(:version => 20) do
     t.column "quantity",       :integer
   end
 
     t.column "quantity",       :integer
   end
 
+  create_table "rentable_policies", :force => true do |t|
+    t.column "name",        :string,  :null => false
+    t.column "value",       :integer
+    t.column "description", :string,  :null => false
+  end
+
   create_table "rentables", :force => true do |t|
     t.column "type",        :string
     t.column "title",       :string
   create_table "rentables", :force => true do |t|
     t.column "type",        :string
     t.column "title",       :string
diff --git a/test/fixtures/rentable_policies.yml b/test/fixtures/rentable_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/rentable_policy_controller_test.rb b/test/functional/rentable_policy_controller_test.rb
new file mode 100644 (file)
index 0000000..aa4ecae
--- /dev/null
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'rentable_policy_controller'
+
+# Re-raise errors caught by the controller.
+class RentablePolicyController; def rescue_action(e) raise e end; end
+
+class RentablePolicyControllerTest < Test::Unit::TestCase
+  fixtures :rentable_policies
+
+  def setup
+    @controller = RentablePolicyController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = rentable_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(:rentable_policies)
+  end
+
+  def test_show
+    get :show, :id => @first_id
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:rentable_policy)
+    assert assigns(:rentable_policy).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:rentable_policy)
+  end
+
+  def test_create
+    num_rentable_policies = RentablePolicy.count
+
+    post :create, :rentable_policy => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_rentable_policies + 1, RentablePolicy.count
+  end
+
+  def test_edit
+    get :edit, :id => @first_id
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:rentable_policy)
+    assert assigns(:rentable_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 {
+      RentablePolicy.find(@first_id)
+    }
+
+    post :destroy, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      RentablePolicy.find(@first_id)
+    }
+  end
+end
diff --git a/test/unit/rentable_policy_test.rb b/test/unit/rentable_policy_test.rb
new file mode 100644 (file)
index 0000000..c58fc1e
--- /dev/null
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class RentablePolicyTest < Test::Unit::TestCase
+  fixtures :rentable_policies
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end