From 4930bd87730032786d9cc7f3d5c1d7cd1f5091bc Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 11:23:12 -0800 Subject: [PATCH] Add coitem MVC Signed-off-by: Ira W. Snyder --- app/controllers/coitem_controller.rb | 51 ++++++++++++ app/helpers/coitem_helper.rb | 2 + app/models/coitem.rb | 12 +++ app/models/customer.rb | 1 + app/models/rentable.rb | 4 + app/views/coitem/_form.rhtml | 16 ++++ app/views/coitem/edit.rhtml | 9 +++ app/views/coitem/list.rhtml | 31 ++++++++ app/views/coitem/new.rhtml | 8 ++ app/views/coitem/show.rhtml | 8 ++ app/views/layouts/coitem.rhtml | 17 ++++ db/development.sqlite3 | Bin 4096 -> 5120 bytes db/migrate/003_create_coitems.rb | 14 ++++ db/schema.rb | 9 ++- log/development.log | 0 log/production.log | 0 log/server.log | 0 log/test.log | 0 test/fixtures/coitems.yml | 5 ++ test/functional/coitem_controller_test.rb | 92 ++++++++++++++++++++++ test/unit/coitem_test.rb | 10 +++ 21 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 app/controllers/coitem_controller.rb create mode 100644 app/helpers/coitem_helper.rb create mode 100644 app/models/coitem.rb create mode 100644 app/views/coitem/_form.rhtml create mode 100644 app/views/coitem/edit.rhtml create mode 100644 app/views/coitem/list.rhtml create mode 100644 app/views/coitem/new.rhtml create mode 100644 app/views/coitem/show.rhtml create mode 100644 app/views/layouts/coitem.rhtml create mode 100644 db/migrate/003_create_coitems.rb delete mode 100644 log/development.log delete mode 100644 log/production.log delete mode 100644 log/server.log delete mode 100644 log/test.log create mode 100644 test/fixtures/coitems.yml create mode 100644 test/functional/coitem_controller_test.rb create mode 100644 test/unit/coitem_test.rb diff --git a/app/controllers/coitem_controller.rb b/app/controllers/coitem_controller.rb new file mode 100644 index 0000000..224806d --- /dev/null +++ b/app/controllers/coitem_controller.rb @@ -0,0 +1,51 @@ +class CoitemController < 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 + @coitem_pages, @coitems = paginate :coitems, :per_page => 10 + end + + def show + @coitem = Coitem.find(params[:id]) + end + + def new + @coitem = Coitem.new + end + + def create + @coitem = Coitem.new(params[:coitem]) + if @coitem.save + flash[:notice] = 'Coitem was successfully created.' + redirect_to :action => 'list' + else + render :action => 'new' + end + end + + def edit + @coitem = Coitem.find(params[:id]) + end + + def update + @coitem = Coitem.find(params[:id]) + if @coitem.update_attributes(params[:coitem]) + flash[:notice] = 'Coitem was successfully updated.' + redirect_to :action => 'show', :id => @coitem + else + render :action => 'edit' + end + end + + def destroy + Coitem.find(params[:id]).destroy + redirect_to :action => 'list' + end +end diff --git a/app/helpers/coitem_helper.rb b/app/helpers/coitem_helper.rb new file mode 100644 index 0000000..5784567 --- /dev/null +++ b/app/helpers/coitem_helper.rb @@ -0,0 +1,2 @@ +module CoitemHelper +end diff --git a/app/models/coitem.rb b/app/models/coitem.rb new file mode 100644 index 0000000..e77f573 --- /dev/null +++ b/app/models/coitem.rb @@ -0,0 +1,12 @@ +class Coitem < ActiveRecord::Base + has_one :customer + has_one :rentable + + validates_presence_of :customer_id + validates_presence_of :rentable_id + + validates_uniqueness_of :rentable_id + + validates_associated :customer + validates_associated :rentable +end diff --git a/app/models/customer.rb b/app/models/customer.rb index 760a3dc..11411bb 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,5 @@ class Customer < ActiveRecord::Base + belongs_to :coitem # FIXME: I hunch this is wrong validates_presence_of :name, :email, :phone, :address validates_numericality_of :debt diff --git a/app/models/rentable.rb b/app/models/rentable.rb index d7ec5f9..faef2c5 100644 --- a/app/models/rentable.rb +++ b/app/models/rentable.rb @@ -1,2 +1,6 @@ class Rentable < ActiveRecord::Base + belongs_to :coitem # FIXME: I hunch this is wrong + + validates_presence_of :title + validates_presence_of :genre end diff --git a/app/views/coitem/_form.rhtml b/app/views/coitem/_form.rhtml new file mode 100644 index 0000000..6cf12a5 --- /dev/null +++ b/app/views/coitem/_form.rhtml @@ -0,0 +1,16 @@ +<%= error_messages_for 'coitem' %> + + +


+<%= select 'coitem', 'customer_id', Customer.find(:all).collect {|c| [c.name.to_s, c.id] } %>

+ +


+<%= select 'coitem', 'rentable_id', Rentable.find(:all).collect {|r| [r.title.to_s, r.id] } %>

+ +


+<%= date_select 'coitem', 'out_date' %>

+ +


+<%= date_select 'coitem', 'due_date' %>

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

Editing coitem

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

Listing coitems

+ + + + + + <% for column in Coitem.content_columns %> + + <% end %> + + +<% for coitem in @coitems %> + + + + <% for column in Coitem.content_columns %> + + <% end %> + + + + +<% end %> +
CustomerRentable<%= column.human_name %>
<%=h Customer.find(coitem.customer_id).name %><%=h Rentable.find(coitem.rentable_id).title %><%=h coitem.send(column.name) %><%= link_to 'Show', :action => 'show', :id => coitem %><%= link_to 'Edit', :action => 'edit', :id => coitem %><%= link_to 'Destroy', { :action => 'destroy', :id => coitem }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= link_to 'Previous page', { :page => @coitem_pages.current.previous } if @coitem_pages.current.previous %> +<%= link_to 'Next page', { :page => @coitem_pages.current.next } if @coitem_pages.current.next %> + +
+ +<%= link_to 'New coitem', :action => 'new' %> diff --git a/app/views/coitem/new.rhtml b/app/views/coitem/new.rhtml new file mode 100644 index 0000000..a494968 --- /dev/null +++ b/app/views/coitem/new.rhtml @@ -0,0 +1,8 @@ +

New coitem

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

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

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

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/db/development.sqlite3 b/db/development.sqlite3 index cb4040e13aadfe5abd8f595437b4c245dca68927..833c5326679edd286ac36f8b3d940fbc921212cf 100644 GIT binary patch delta 396 zcmZorXwaA-Ehxo+3RouU*zmA0$}%u-Vp_$-!6-Yiaiw8n1`E5mv@~P0dP!nZPHJ*~ zW=U#pF_dC;4svx2aa9O$bnvdjoEILkza6)KBeFAGGLi3zU55Y93O0JCRZbpQYW delta 53 wcmZqBXi%6SEy&A&3YaJA*l;m3aWF7%Vp_$-v00F18slaTrthqbOp7=;0K}mPT>t<8 diff --git a/db/migrate/003_create_coitems.rb b/db/migrate/003_create_coitems.rb new file mode 100644 index 0000000..5c6e78b --- /dev/null +++ b/db/migrate/003_create_coitems.rb @@ -0,0 +1,14 @@ +class CreateCoitems < ActiveRecord::Migration + def self.up + create_table :coitems do |t| + t.column :customer_id, :integer + t.column :rentable_id, :integer + t.column :out_date, :date, :default => Time.now + t.column :due_date, :date + end + end + + def self.down + drop_table :coitems + end +end diff --git a/db/schema.rb b/db/schema.rb index dbb0282..13107c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,14 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 2) do +ActiveRecord::Schema.define(:version => 3) do + + create_table "coitems", :force => true do |t| + t.column "customer_id", :integer + t.column "rentable_id", :integer + t.column "out_date", :date, :default => '2007-11-21' + t.column "due_date", :date + end create_table "customers", :force => true do |t| t.column "name", :string diff --git a/log/development.log b/log/development.log deleted file mode 100644 index e69de29..0000000 diff --git a/log/production.log b/log/production.log deleted file mode 100644 index e69de29..0000000 diff --git a/log/server.log b/log/server.log deleted file mode 100644 index e69de29..0000000 diff --git a/log/test.log b/log/test.log deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/coitems.yml b/test/fixtures/coitems.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/coitems.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/coitem_controller_test.rb b/test/functional/coitem_controller_test.rb new file mode 100644 index 0000000..4d7e8b8 --- /dev/null +++ b/test/functional/coitem_controller_test.rb @@ -0,0 +1,92 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'coitem_controller' + +# Re-raise errors caught by the controller. +class CoitemController; def rescue_action(e) raise e end; end + +class CoitemControllerTest < Test::Unit::TestCase + fixtures :coitems + + def setup + @controller = CoitemController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @first_id = coitems(: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(:coitems) + end + + def test_show + get :show, :id => @first_id + + assert_response :success + assert_template 'show' + + assert_not_nil assigns(:coitem) + assert assigns(:coitem).valid? + end + + def test_new + get :new + + assert_response :success + assert_template 'new' + + assert_not_nil assigns(:coitem) + end + + def test_create + num_coitems = Coitem.count + + post :create, :coitem => {} + + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_equal num_coitems + 1, Coitem.count + end + + def test_edit + get :edit, :id => @first_id + + assert_response :success + assert_template 'edit' + + assert_not_nil assigns(:coitem) + assert assigns(:coitem).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 { + Coitem.find(@first_id) + } + + post :destroy, :id => @first_id + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_raise(ActiveRecord::RecordNotFound) { + Coitem.find(@first_id) + } + end +end diff --git a/test/unit/coitem_test.rb b/test/unit/coitem_test.rb new file mode 100644 index 0000000..e130313 --- /dev/null +++ b/test/unit/coitem_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CoitemTest < Test::Unit::TestCase + fixtures :coitems + + # Replace this with your real tests. + def test_truth + assert true + end +end -- 2.25.1