--- /dev/null
+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
--- /dev/null
+module CoitemHelper
+end
--- /dev/null
+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
class Customer < ActiveRecord::Base
+ belongs_to :coitem # FIXME: I hunch this is wrong
validates_presence_of :name, :email, :phone, :address
validates_numericality_of :debt
class Rentable < ActiveRecord::Base
+ belongs_to :coitem # FIXME: I hunch this is wrong
+
+ validates_presence_of :title
+ validates_presence_of :genre
end
--- /dev/null
+<%= error_messages_for 'coitem' %>
+
+<!--[form:coitem]-->
+<p><label for="coitem_customer_id">Customer</label><br/>
+<%= select 'coitem', 'customer_id', Customer.find(:all).collect {|c| [c.name.to_s, c.id] } %></p>
+
+<p><label for="coitem_rentable_id">Rentable</label><br/>
+<%= select 'coitem', 'rentable_id', Rentable.find(:all).collect {|r| [r.title.to_s, r.id] } %></p>
+
+<p><label for="coitem_out_date">Out date</label><br/>
+<%= date_select 'coitem', 'out_date' %></p>
+
+<p><label for="coitem_due_date">Due date</label><br/>
+<%= date_select 'coitem', 'due_date' %></p>
+<!--[eoform:coitem]-->
+
--- /dev/null
+<h1>Editing coitem</h1>
+
+<% 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' %>
--- /dev/null
+<h1>Listing coitems</h1>
+
+<table>
+ <tr>
+ <th>Customer</th>
+ <th>Rentable</th>
+ <% for column in Coitem.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for coitem in @coitems %>
+ <tr>
+ <td><%=h Customer.find(coitem.customer_id).name %></td>
+ <td><%=h Rentable.find(coitem.rentable_id).title %></td>
+ <% for column in Coitem.content_columns %>
+ <td><%=h coitem.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => coitem %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => coitem %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => coitem }, :confirm => 'Are you sure?', :method => :post %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= 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 %>
+
+<br />
+
+<%= link_to 'New coitem', :action => 'new' %>
--- /dev/null
+<h1>New coitem</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% for column in Coitem.content_columns %>
+<p>
+ <b><%= column.human_name %>:</b> <%=h @coitem.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @coitem %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<!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>Coitem: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
--- /dev/null
+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
# 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
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+two:
+ id: 2
--- /dev/null
+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
--- /dev/null
+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