--- /dev/null
+class BitemController < 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
+ @bitem_pages, @bitems = paginate :bitems, :per_page => 10
+ end
+
+ def show
+ @bitem = Bitem.find(params[:id])
+ end
+
+ def new
+ @bitem = Bitem.new
+ end
+
+ def create
+ @bitem = Bitem.new(params[:bitem])
+ if @bitem.save
+ flash[:notice] = 'Bitem was successfully created.'
+ redirect_to :action => 'list'
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @bitem = Bitem.find(params[:id])
+ end
+
+ def update
+ @bitem = Bitem.find(params[:id])
+ if @bitem.update_attributes(params[:bitem])
+ flash[:notice] = 'Bitem was successfully updated.'
+ redirect_to :action => 'show', :id => @bitem
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ Bitem.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
--- /dev/null
+module BitemHelper
+end
--- /dev/null
+class Bitem < ActiveRecord::Base
+ belongs_to :customer
+ belongs_to :merchandise
+
+ validates_presence_of :date
+ validates_presence_of :customer_id
+ validates_presence_of :merchandise_id
+ validates_numericality_of :customer_id
+ validates_numericality_of :merchandise_id
+ validates_associated :customer
+ validates_associated :merchandise
+
+ protected
+ def validate
+ errors.add(:customer_id, "does not exist is the database") if customer.nil?
+ errors.add(:merchandise_id, "does not exist in the database") if merchandise.nil?
+ end
+end
class Customer < ActiveRecord::Base
- has_many :coitem
+ has_many :coitems
+ has_many :bitems
validates_presence_of :name, :email, :phone, :address
validates_numericality_of :debt
--- /dev/null
+<%= error_messages_for 'bitem' %>
+
+<!--[form:bitem]-->
+<p><label for="bitem_customer_id">Customer ID</label><br/>
+<%= text_field 'bitem', 'customer_id' %></p>
+
+<p><label for="bitem_merchandise_id">Merchandise ID</label><br/>
+<%= text_field 'bitem', 'merchandise_id' %></p>
+
+<p><label for="bitem_date">Date</label><br/>
+<%= date_select 'bitem', 'date' %></p>
+<!--[eoform:bitem]-->
+
--- /dev/null
+<h1>Editing bitem</h1>
+
+<% form_tag :action => 'update', :id => @bitem do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @bitem %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<h1>Listing bitems</h1>
+
+<table>
+ <tr>
+ <th>Customer Name</th>
+ <th>Merchandise Name</th>
+ <% for column in Bitem.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for bitem in @bitems %>
+ <tr>
+ <td><%=h bitem.customer.name %></td>
+ <td><%=h bitem.merchandise.name %></td>
+ <% for column in Bitem.content_columns %>
+ <td><%=h bitem.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => bitem %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => bitem %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => bitem }, :confirm => 'Are you sure?', :method => :post %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @bitem_pages.current.previous } if @bitem_pages.current.previous %>
+<%= link_to 'Next page', { :page => @bitem_pages.current.next } if @bitem_pages.current.next %>
+
+<br />
+
+<%= link_to 'New bitem', :action => 'new' %>
--- /dev/null
+<h1>New bitem</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% for column in Bitem.content_columns %>
+<p>
+ <b><%= column.human_name %>:</b> <%=h @bitem.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @bitem %> |
+<%= 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>Bitem: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
<table>
<tr>
+ <th>Merchandise ID</th>
<% for column in Merchandise.content_columns %>
<th><%= column.human_name %></th>
<% end %>
<% for merchandise in @merchandises %>
<tr>
+ <td><%=h merchandise.id %></td>
<% for column in Merchandise.content_columns %>
<td><%=h merchandise.send(column.name) %></td>
<% end %>
--- /dev/null
+class CreateBitems < ActiveRecord::Migration
+ def self.up
+ create_table :bitems do |t|
+ t.column :customer_id, :integer, :null => false
+ t.column :merchandise_id, :integer, :null => false
+ t.column :date, :date, :null => false
+ end
+ end
+
+ def self.down
+ drop_table :bitems
+ end
+end
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
-ActiveRecord::Schema.define(:version => 14) do
+ActiveRecord::Schema.define(:version => 15) do
+
+ create_table "bitems", :force => true do |t|
+ t.column "customer_id", :integer, :null => false
+ t.column "merchandise_id", :integer, :null => false
+ t.column "date", :date, :null => false
+ end
create_table "coitems", :force => true do |t|
t.column "customer_id", :integer
--- /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 'bitem_controller'
+
+# Re-raise errors caught by the controller.
+class BitemController; def rescue_action(e) raise e end; end
+
+class BitemControllerTest < Test::Unit::TestCase
+ fixtures :bitems
+
+ def setup
+ @controller = BitemController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @first_id = bitems(: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(:bitems)
+ end
+
+ def test_show
+ get :show, :id => @first_id
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:bitem)
+ assert assigns(:bitem).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:bitem)
+ end
+
+ def test_create
+ num_bitems = Bitem.count
+
+ post :create, :bitem => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_bitems + 1, Bitem.count
+ end
+
+ def test_edit
+ get :edit, :id => @first_id
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:bitem)
+ assert assigns(:bitem).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 {
+ Bitem.find(@first_id)
+ }
+
+ post :destroy, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Bitem.find(@first_id)
+ }
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class BitemTest < Test::Unit::TestCase
+ fixtures :bitems
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end