From f09365a8d79a8a4a756fdba732bb753829df0df2 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 07:50:38 -0800 Subject: [PATCH] Add customers model-view-controller Signed-off-by: Ira W. Snyder --- app/controllers/customer_controller.rb | 51 +++++++++++ app/helpers/customer_helper.rb | 2 + app/models/customer.rb | 12 +++ app/views/customer/_form.rhtml | 19 ++++ app/views/customer/edit.rhtml | 9 ++ app/views/customer/list.rhtml | 27 ++++++ app/views/customer/new.rhtml | 8 ++ app/views/customer/show.rhtml | 8 ++ app/views/layouts/customer.rhtml | 17 ++++ db/development.sqlite3 | Bin 0 -> 3072 bytes db/migrate/001_create_customers.rb | 15 ++++ db/schema.rb | 15 ++++ test/fixtures/customers.yml | 5 ++ test/functional/customer_controller_test.rb | 92 ++++++++++++++++++++ test/unit/customer_test.rb | 10 +++ 15 files changed, 290 insertions(+) create mode 100644 app/controllers/customer_controller.rb create mode 100644 app/helpers/customer_helper.rb create mode 100644 app/models/customer.rb create mode 100644 app/views/customer/_form.rhtml create mode 100644 app/views/customer/edit.rhtml create mode 100644 app/views/customer/list.rhtml create mode 100644 app/views/customer/new.rhtml create mode 100644 app/views/customer/show.rhtml create mode 100644 app/views/layouts/customer.rhtml create mode 100644 db/development.sqlite3 create mode 100644 db/migrate/001_create_customers.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/customers.yml create mode 100644 test/functional/customer_controller_test.rb create mode 100644 test/unit/customer_test.rb diff --git a/app/controllers/customer_controller.rb b/app/controllers/customer_controller.rb new file mode 100644 index 0000000..6f7c8ee --- /dev/null +++ b/app/controllers/customer_controller.rb @@ -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 index 0000000..ec7116d --- /dev/null +++ b/app/helpers/customer_helper.rb @@ -0,0 +1,2 @@ +module CustomerHelper +end diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 0000000..760a3dc --- /dev/null +++ b/app/models/customer.rb @@ -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 index 0000000..df2a4b0 --- /dev/null +++ b/app/views/customer/_form.rhtml @@ -0,0 +1,19 @@ +<%= error_messages_for 'customer' %> + + +


+<%= text_field 'customer', 'name' %>

+ +


+<%= text_field 'customer', 'address' %>

+ +


+<%= text_field 'customer', 'email' %>

+ +


+<%= text_field 'customer', 'phone' %>

+ +


+<%= text_field 'customer', 'debt' %>

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

Editing customer

+ +<% 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 index 0000000..fb29a65 --- /dev/null +++ b/app/views/customer/list.rhtml @@ -0,0 +1,27 @@ +

Listing customers

+ + + + <% for column in Customer.content_columns %> + + <% end %> + + +<% for customer in @customers %> + + <% for column in Customer.content_columns %> + + <% end %> + + + + +<% end %> +
<%= column.human_name %>
<%=h customer.send(column.name) %><%= link_to 'Show', :action => 'show', :id => customer %><%= link_to 'Edit', :action => 'edit', :id => customer %><%= link_to 'Destroy', { :action => 'destroy', :id => customer }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= 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 %> + +
+ +<%= link_to 'New customer', :action => 'new' %> diff --git a/app/views/customer/new.rhtml b/app/views/customer/new.rhtml new file mode 100644 index 0000000..dd49e75 --- /dev/null +++ b/app/views/customer/new.rhtml @@ -0,0 +1,8 @@ +

New customer

+ +<% 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 index 0000000..fe1aba6 --- /dev/null +++ b/app/views/customer/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Customer.content_columns %> +

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

+<% 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 index 0000000..1f8776b --- /dev/null +++ b/app/views/layouts/customer.rhtml @@ -0,0 +1,17 @@ + + + + + + Customer: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/db/development.sqlite3 b/db/development.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..cd6d3677d35c1f3faaeadd04445a7b001482493a GIT binary patch literal 3072 zcmeH|O-sW-5QcXXwO%}X?3hw$p?>wlw6{hZr5I~XTcjr$lhrOHS=mIvtNt6k`S-lI ztym+1q8`n#Gt9m_%q$NqJ2-K8!r(&45EH181wtul0}w)%m8VTp8=4Z*c}o7At|&>< z9*F*=LS`9(UlF)l)N-r&JiQ+$cpfnCI!;8$WIRvwJ=eB|HVmy@$A-BKC4>70bbCYl zz;>bUc8@G~42Sj@ddEZPjU1-}MuZ_V;0C2PL0PIc8fEC%`_{;rSD^1p7RO%~u@HIi zb;;F4MBj|^*?D4s&paMtP-<4H@0ZysJ9KHaP@p)Y5PK6f@{C6pB9&?%NTd#+bfbuI z5dn`9cFAOU`ZlTlOw`IOBajjJKLm8DQ9ApZb(iRmDrA-s$O!zAz^RraYa_ErI~c(r zy7if?*GxF%B8U_8p(8h-AE3vggkuHvENE4m=9bUmS?!sJ?OU7(vEhl(+^(-Ts#C9- gWI(5lH;c3@@du5$#J2L+?r%+-JB{^Ly|qnV0HAKB7XSbN literal 0 HcmV?d00001 diff --git a/db/migrate/001_create_customers.rb b/db/migrate/001_create_customers.rb new file mode 100644 index 0000000..033a480 --- /dev/null +++ b/db/migrate/001_create_customers.rb @@ -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 index 0000000..f117726 --- /dev/null +++ b/db/schema.rb @@ -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 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/customers.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/customer_controller_test.rb b/test/functional/customer_controller_test.rb new file mode 100644 index 0000000..255e041 --- /dev/null +++ b/test/functional/customer_controller_test.rb @@ -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 index 0000000..35d8673 --- /dev/null +++ b/test/unit/customer_test.rb @@ -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 -- 2.25.1