From: Ira W. Snyder Date: Thu, 22 Nov 2007 20:48:33 +0000 (-0800) Subject: Add Bitem (bought items) MVC X-Git-Tag: turned-in~61 X-Git-Url: https://irasnyder.com/gitweb/?a=commitdiff_plain;ds=sidebyside;h=6de5aedfe8ff0bc967b3690ae53d1f4205c10798;hp=f4a463b2d77fccc4919aea19a54736db6e485688;p=cs356-p2-videostore.git Add Bitem (bought items) MVC Signed-off-by: Ira W. Snyder --- 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 7b507a5..10e50e7 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ 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
Merchandise ID<%= column.human_name %>
<%=h merchandise.id %><%=h merchandise.send(column.name) %>