From 6de5aedfe8ff0bc967b3690ae53d1f4205c10798 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Thu, 22 Nov 2007 12:48:33 -0800 Subject: [PATCH] Add Bitem (bought items) MVC Signed-off-by: Ira W. Snyder --- app/controllers/bitem_controller.rb | 51 +++++++++++++ app/helpers/bitem_helper.rb | 2 + app/models/bitem.rb | 18 +++++ app/models/customer.rb | 3 +- app/views/bitem/_form.rhtml | 13 ++++ app/views/bitem/edit.rhtml | 9 +++ app/views/bitem/list.rhtml | 31 ++++++++ app/views/bitem/new.rhtml | 8 ++ app/views/bitem/show.rhtml | 8 ++ app/views/layouts/bitem.rhtml | 17 +++++ app/views/merchandise/list.rhtml | 2 + db/development.sqlite3 | Bin 10240 -> 11264 bytes db/migrate/015_create_bitems.rb | 13 ++++ db/schema.rb | 8 +- test/fixtures/bitems.yml | 5 ++ test/functional/bitem_controller_test.rb | 92 +++++++++++++++++++++++ test/unit/bitem_test.rb | 10 +++ 17 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 app/controllers/bitem_controller.rb create mode 100644 app/helpers/bitem_helper.rb create mode 100644 app/models/bitem.rb create mode 100644 app/views/bitem/_form.rhtml create mode 100644 app/views/bitem/edit.rhtml create mode 100644 app/views/bitem/list.rhtml create mode 100644 app/views/bitem/new.rhtml create mode 100644 app/views/bitem/show.rhtml create mode 100644 app/views/layouts/bitem.rhtml create mode 100644 db/migrate/015_create_bitems.rb create mode 100644 test/fixtures/bitems.yml create mode 100644 test/functional/bitem_controller_test.rb create mode 100644 test/unit/bitem_test.rb diff --git a/app/controllers/bitem_controller.rb b/app/controllers/bitem_controller.rb new file mode 100644 index 0000000..4cae2e6 --- /dev/null +++ b/app/controllers/bitem_controller.rb @@ -0,0 +1,51 @@ +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 diff --git a/app/helpers/bitem_helper.rb b/app/helpers/bitem_helper.rb new file mode 100644 index 0000000..5678936 --- /dev/null +++ b/app/helpers/bitem_helper.rb @@ -0,0 +1,2 @@ +module BitemHelper +end diff --git a/app/models/bitem.rb b/app/models/bitem.rb new file mode 100644 index 0000000..8dfaf81 --- /dev/null +++ b/app/models/bitem.rb @@ -0,0 +1,18 @@ +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 diff --git a/app/models/customer.rb b/app/models/customer.rb index 1a86679..901b9a5 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,5 +1,6 @@ class Customer < ActiveRecord::Base - has_many :coitem + has_many :coitems + has_many :bitems validates_presence_of :name, :email, :phone, :address validates_numericality_of :debt diff --git a/app/views/bitem/_form.rhtml b/app/views/bitem/_form.rhtml new file mode 100644 index 0000000..a3db253 --- /dev/null +++ b/app/views/bitem/_form.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'bitem' %> + + +


+<%= text_field 'bitem', 'customer_id' %>

+ +


+<%= text_field 'bitem', 'merchandise_id' %>

+ +


+<%= date_select 'bitem', 'date' %>

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

Editing bitem

+ +<% 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' %> diff --git a/app/views/bitem/list.rhtml b/app/views/bitem/list.rhtml new file mode 100644 index 0000000..6b5bb14 --- /dev/null +++ b/app/views/bitem/list.rhtml @@ -0,0 +1,31 @@ +

Listing bitems

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

New bitem

+ +<% form_tag :action => 'create' do %> + <%= render :partial => 'form' %> + <%= submit_tag "Create" %> +<% end %> + +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/bitem/show.rhtml b/app/views/bitem/show.rhtml new file mode 100644 index 0000000..fd2d14b --- /dev/null +++ b/app/views/bitem/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Bitem.content_columns %> +

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

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @bitem %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/layouts/bitem.rhtml b/app/views/layouts/bitem.rhtml new file mode 100644 index 0000000..fc00ace --- /dev/null +++ b/app/views/layouts/bitem.rhtml @@ -0,0 +1,17 @@ + + + + + + Bitem: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/merchandise/list.rhtml b/app/views/merchandise/list.rhtml index c8a1ba0..a328f09 100644 --- a/app/views/merchandise/list.rhtml +++ b/app/views/merchandise/list.rhtml @@ -2,6 +2,7 @@ + <% for column in Merchandise.content_columns %> <% end %> @@ -9,6 +10,7 @@ <% for merchandise in @merchandises %> + <% for column in Merchandise.content_columns %> <% end %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 7b507a52503d1cf5d81c2f515c3a32ffd2090749..10e50e7d4e68027ab0e2dc78f2efe5e5df8782e3 100644 GIT binary patch delta 228 zcmZn&Xo#2~EqI#&6_{+)xy;VUzlnoIf{&F!hk;3mX(Hnyh8qkz6CI_r8a27t#U&*f zn{-POlX6m%GD}i(iyfthJpq=4=>P-Ow6|!nBOwr+APR& ij#*8RiIstoQIUzs$iTo{*U(Vc$ViY8EX2r&5CQ-!i8kE; delta 55 zcmZpOXb6}fEqIjy6&P>Sxy;VUw~2#Af`^6S1_P52(?rHa3^yh=p4fatL4zFtG6oFC diff --git a/db/migrate/015_create_bitems.rb b/db/migrate/015_create_bitems.rb new file mode 100644 index 0000000..641c2c9 --- /dev/null +++ b/db/migrate/015_create_bitems.rb @@ -0,0 +1,13 @@ +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 diff --git a/db/schema.rb b/db/schema.rb index 73ad8b3..dcfe7d9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,13 @@ # 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 diff --git a/test/fixtures/bitems.yml b/test/fixtures/bitems.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/bitems.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/bitem_controller_test.rb b/test/functional/bitem_controller_test.rb new file mode 100644 index 0000000..72063e0 --- /dev/null +++ b/test/functional/bitem_controller_test.rb @@ -0,0 +1,92 @@ +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 diff --git a/test/unit/bitem_test.rb b/test/unit/bitem_test.rb new file mode 100644 index 0000000..966a42b --- /dev/null +++ b/test/unit/bitem_test.rb @@ -0,0 +1,10 @@ +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 -- 2.25.1
Merchandise ID<%= column.human_name %>
<%=h merchandise.id %><%=h merchandise.send(column.name) %>