Add customers model-view-controller
authorIra W. Snyder <devel@irasnyder.com>
Wed, 21 Nov 2007 15:50:38 +0000 (07:50 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 21 Nov 2007 15:50:38 +0000 (07:50 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
15 files changed:
app/controllers/customer_controller.rb [new file with mode: 0644]
app/helpers/customer_helper.rb [new file with mode: 0644]
app/models/customer.rb [new file with mode: 0644]
app/views/customer/_form.rhtml [new file with mode: 0644]
app/views/customer/edit.rhtml [new file with mode: 0644]
app/views/customer/list.rhtml [new file with mode: 0644]
app/views/customer/new.rhtml [new file with mode: 0644]
app/views/customer/show.rhtml [new file with mode: 0644]
app/views/layouts/customer.rhtml [new file with mode: 0644]
db/development.sqlite3 [new file with mode: 0644]
db/migrate/001_create_customers.rb [new file with mode: 0644]
db/schema.rb [new file with mode: 0644]
test/fixtures/customers.yml [new file with mode: 0644]
test/functional/customer_controller_test.rb [new file with mode: 0644]
test/unit/customer_test.rb [new file with mode: 0644]

diff --git a/app/controllers/customer_controller.rb b/app/controllers/customer_controller.rb
new file mode 100644 (file)
index 0000000..6f7c8ee
--- /dev/null
@@ -0,0 +1,51 @@
+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
diff --git a/app/helpers/customer_helper.rb b/app/helpers/customer_helper.rb
new file mode 100644 (file)
index 0000000..ec7116d
--- /dev/null
@@ -0,0 +1,2 @@
+module CustomerHelper
+end
diff --git a/app/models/customer.rb b/app/models/customer.rb
new file mode 100644 (file)
index 0000000..760a3dc
--- /dev/null
@@ -0,0 +1,12 @@
+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
diff --git a/app/views/customer/_form.rhtml b/app/views/customer/_form.rhtml
new file mode 100644 (file)
index 0000000..df2a4b0
--- /dev/null
@@ -0,0 +1,19 @@
+<%= 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]-->
+
diff --git a/app/views/customer/edit.rhtml b/app/views/customer/edit.rhtml
new file mode 100644 (file)
index 0000000..eb4801a
--- /dev/null
@@ -0,0 +1,9 @@
+<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' %>
diff --git a/app/views/customer/list.rhtml b/app/views/customer/list.rhtml
new file mode 100644 (file)
index 0000000..fb29a65
--- /dev/null
@@ -0,0 +1,27 @@
+<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' %>
diff --git a/app/views/customer/new.rhtml b/app/views/customer/new.rhtml
new file mode 100644 (file)
index 0000000..dd49e75
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New customer</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/customer/show.rhtml b/app/views/customer/show.rhtml
new file mode 100644 (file)
index 0000000..fe1aba6
--- /dev/null
@@ -0,0 +1,8 @@
+<% 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' %>
diff --git a/app/views/layouts/customer.rhtml b/app/views/layouts/customer.rhtml
new file mode 100644 (file)
index 0000000..1f8776b
--- /dev/null
@@ -0,0 +1,17 @@
+<!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>
diff --git a/db/development.sqlite3 b/db/development.sqlite3
new file mode 100644 (file)
index 0000000..cd6d367
Binary files /dev/null and b/db/development.sqlite3 differ
diff --git a/db/migrate/001_create_customers.rb b/db/migrate/001_create_customers.rb
new file mode 100644 (file)
index 0000000..033a480
--- /dev/null
@@ -0,0 +1,15 @@
+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
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644 (file)
index 0000000..f117726
--- /dev/null
@@ -0,0 +1,15 @@
+# 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
diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml
new file mode 100644 (file)
index 0000000..b49c4eb
--- /dev/null
@@ -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/customer_controller_test.rb b/test/functional/customer_controller_test.rb
new file mode 100644 (file)
index 0000000..255e041
--- /dev/null
@@ -0,0 +1,92 @@
+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
diff --git a/test/unit/customer_test.rb b/test/unit/customer_test.rb
new file mode 100644 (file)
index 0000000..35d8673
--- /dev/null
@@ -0,0 +1,10 @@
+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