--- /dev/null
+class CustomerController < 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
+ @customer_pages, @customers = paginate :customers, :per_page => 10
+ end
+
+ def show
+ @customer = Customer.find(params[:id])
+ end
+
+ def new
+ @customer = Customer.new
+ end
+
+ def create
+ @customer = Customer.new(params[:customer])
+ if @customer.save
+ flash[:notice] = 'Customer was successfully created.'
+ redirect_to :action => 'list'
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @customer = Customer.find(params[:id])
+ end
+
+ def update
+ @customer = Customer.find(params[:id])
+ if @customer.update_attributes(params[:customer])
+ flash[:notice] = 'Customer was successfully updated.'
+ redirect_to :action => 'show', :id => @customer
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ Customer.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
--- /dev/null
+module CustomerHelper
+end
--- /dev/null
+class Customer < ActiveRecord::Base
+
+ validates_presence_of :name, :email, :phone, :address
+ validates_numericality_of :debt
+
+ protected
+
+ def validate
+ errors.add(:debt, "should be non-negative") if debt.nil? || debt < 0.00
+ end
+
+end
--- /dev/null
+<%= error_messages_for 'customer' %>
+
+<!--[form:customer]-->
+<p><label for="customer_name">Name</label><br/>
+<%= text_field 'customer', 'name' %></p>
+
+<p><label for="customer_address">Address</label><br/>
+<%= text_field 'customer', 'address' %></p>
+
+<p><label for="customer_email">Email</label><br/>
+<%= text_field 'customer', 'email' %></p>
+
+<p><label for="customer_phone">Phone</label><br/>
+<%= text_field 'customer', 'phone' %></p>
+
+<p><label for="customer_debt">Debt</label><br/>
+<%= text_field 'customer', 'debt' %></p>
+<!--[eoform:customer]-->
+
--- /dev/null
+<h1>Editing customer</h1>
+
+<% form_tag :action => 'update', :id => @customer do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @customer %> |
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<h1>Listing customers</h1>
+
+<table>
+ <tr>
+ <% for column in Customer.content_columns %>
+ <th><%= column.human_name %></th>
+ <% end %>
+ </tr>
+
+<% for customer in @customers %>
+ <tr>
+ <% for column in Customer.content_columns %>
+ <td><%=h customer.send(column.name) %></td>
+ <% end %>
+ <td><%= link_to 'Show', :action => 'show', :id => customer %></td>
+ <td><%= link_to 'Edit', :action => 'edit', :id => customer %></td>
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => customer }, :confirm => 'Are you sure?', :method => :post %></td>
+ </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @customer_pages.current.previous } if @customer_pages.current.previous %>
+<%= link_to 'Next page', { :page => @customer_pages.current.next } if @customer_pages.current.next %>
+
+<br />
+
+<%= link_to 'New customer', :action => 'new' %>
--- /dev/null
+<h1>New customer</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% for column in Customer.content_columns %>
+<p>
+ <b><%= column.human_name %>:</b> <%=h @customer.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @customer %> |
+<%= 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>Customer: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
--- /dev/null
+class CreateCustomers < ActiveRecord::Migration
+ def self.up
+ create_table :customers do |t|
+ t.column :name, :string
+ t.column :address, :string
+ t.column :email, :string
+ t.column :phone, :string
+ t.column :debt, :decimal, :precision => 8, :scale => 2, :default => 0
+ end
+ end
+
+ def self.down
+ drop_table :customers
+ end
+end
--- /dev/null
+# This file is autogenerated. Instead of editing this file, please use the
+# migrations feature of ActiveRecord to incrementally modify your database, and
+# then regenerate this schema definition.
+
+ActiveRecord::Schema.define(:version => 1) do
+
+ create_table "customers", :force => true do |t|
+ t.column "name", :string
+ t.column "address", :string
+ t.column "email", :string
+ t.column "phone", :string
+ t.column "debt", :decimal, :precision => 8, :scale => 2, :default => 0.0
+ end
+
+end
--- /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 'customer_controller'
+
+# Re-raise errors caught by the controller.
+class CustomerController; def rescue_action(e) raise e end; end
+
+class CustomerControllerTest < Test::Unit::TestCase
+ fixtures :customers
+
+ def setup
+ @controller = CustomerController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @first_id = customers(: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(:customers)
+ end
+
+ def test_show
+ get :show, :id => @first_id
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:customer)
+ assert assigns(:customer).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:customer)
+ end
+
+ def test_create
+ num_customers = Customer.count
+
+ post :create, :customer => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_customers + 1, Customer.count
+ end
+
+ def test_edit
+ get :edit, :id => @first_id
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:customer)
+ assert assigns(:customer).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 {
+ Customer.find(@first_id)
+ }
+
+ post :destroy, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Customer.find(@first_id)
+ }
+ end
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class CustomerTest < Test::Unit::TestCase
+ fixtures :customers
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end