From: Ira W. Snyder Date: Fri, 23 Nov 2007 08:50:16 +0000 (-0800) Subject: Add RentablePolicy, to get set up for adding policies X-Git-Tag: turned-in~53 X-Git-Url: https://www.irasnyder.com/gitweb/?p=cs356-p2-videostore.git;a=commitdiff_plain;h=9b264f25255cc0e7cca3169d99905f590c8be578 Add RentablePolicy, to get set up for adding policies Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/rentable_policy_controller.rb b/app/controllers/rentable_policy_controller.rb new file mode 100644 index 0000000..f99fbed --- /dev/null +++ b/app/controllers/rentable_policy_controller.rb @@ -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 index 0000000..cbbd668 --- /dev/null +++ b/app/helpers/rentable_policy_helper.rb @@ -0,0 +1,2 @@ +module RentablePolicyHelper +end diff --git a/app/models/game.rb b/app/models/game.rb index 872b45a..bd1f902 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -9,6 +9,7 @@ class Game < Rentable 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 diff --git a/app/models/rentable_policy.rb b/app/models/rentable_policy.rb new file mode 100644 index 0000000..610f13e --- /dev/null +++ b/app/models/rentable_policy.rb @@ -0,0 +1,2 @@ +class RentablePolicy < ActiveRecord::Base +end diff --git a/app/models/video.rb b/app/models/video.rb index 6f75a26..1f39114 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -13,6 +13,7 @@ class Video < Rentable 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 diff --git a/app/views/layouts/rentable_policy.rhtml b/app/views/layouts/rentable_policy.rhtml new file mode 100644 index 0000000..ac13d73 --- /dev/null +++ b/app/views/layouts/rentable_policy.rhtml @@ -0,0 +1,17 @@ + + + + + + RentablePolicy: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/rentable_policy/_form.rhtml b/app/views/rentable_policy/_form.rhtml new file mode 100644 index 0000000..ad40e56 --- /dev/null +++ b/app/views/rentable_policy/_form.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'rentable_policy' %> + + +


+<%= text_field 'rentable_policy', 'name' %>

+ +


+<%= text_field 'rentable_policy', 'value' %>

+ +


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

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

Editing rentable_policy

+ +<% 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 index 0000000..b5a4833 --- /dev/null +++ b/app/views/rentable_policy/list.rhtml @@ -0,0 +1,27 @@ +

Listing rentable_policies

+ + + + <% for column in RentablePolicy.content_columns %> + + <% end %> + + +<% for rentable_policy in @rentable_policies %> + + <% for column in RentablePolicy.content_columns %> + + <% end %> + + + + +<% end %> +
<%= column.human_name %>
<%=h rentable_policy.send(column.name) %><%= link_to 'Show', :action => 'show', :id => rentable_policy %><%= link_to 'Edit', :action => 'edit', :id => rentable_policy %><%= link_to 'Destroy', { :action => 'destroy', :id => rentable_policy }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= 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 %> + +
+ +<%= 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 index 0000000..1470ff8 --- /dev/null +++ b/app/views/rentable_policy/new.rhtml @@ -0,0 +1,8 @@ +

New rentable_policy

+ +<% 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 index 0000000..273bd34 --- /dev/null +++ b/app/views/rentable_policy/show.rhtml @@ -0,0 +1,8 @@ +<% for column in RentablePolicy.content_columns %> +

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

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @rentable_policy %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index d7f8a65..f7ee02e 100644 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 index 0000000..67adbe5 --- /dev/null +++ b/db/migrate/021_create_rentable_policies.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ae1459b..8132da3 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 => 20) do +ActiveRecord::Schema.define(:version => 21) do 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 + 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 diff --git a/test/fixtures/rentable_policies.yml b/test/fixtures/rentable_policies.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/rentable_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/rentable_policy_controller_test.rb b/test/functional/rentable_policy_controller_test.rb new file mode 100644 index 0000000..aa4ecae --- /dev/null +++ b/test/functional/rentable_policy_controller_test.rb @@ -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 index 0000000..c58fc1e --- /dev/null +++ b/test/unit/rentable_policy_test.rb @@ -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