From: Ira W. Snyder Date: Wed, 21 Nov 2007 15:50:38 +0000 (-0800) Subject: Add customers model-view-controller X-Git-Tag: turned-in~88 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=f09365a8d79a8a4a756fdba732bb753829df0df2;p=cs356-p2-videostore.git Add customers model-view-controller Signed-off-by: Ira W. Snyder --- 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 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 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