Add Bitem (bought items) MVC
authorIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 20:48:33 +0000 (12:48 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 20:48:33 +0000 (12:48 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
17 files changed:
app/controllers/bitem_controller.rb [new file with mode: 0644]
app/helpers/bitem_helper.rb [new file with mode: 0644]
app/models/bitem.rb [new file with mode: 0644]
app/models/customer.rb
app/views/bitem/_form.rhtml [new file with mode: 0644]
app/views/bitem/edit.rhtml [new file with mode: 0644]
app/views/bitem/list.rhtml [new file with mode: 0644]
app/views/bitem/new.rhtml [new file with mode: 0644]
app/views/bitem/show.rhtml [new file with mode: 0644]
app/views/layouts/bitem.rhtml [new file with mode: 0644]
app/views/merchandise/list.rhtml
db/development.sqlite3
db/migrate/015_create_bitems.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/bitems.yml [new file with mode: 0644]
test/functional/bitem_controller_test.rb [new file with mode: 0644]
test/unit/bitem_test.rb [new file with mode: 0644]

diff --git a/app/controllers/bitem_controller.rb b/app/controllers/bitem_controller.rb
new file mode 100644 (file)
index 0000000..4cae2e6
--- /dev/null
@@ -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 (file)
index 0000000..5678936
--- /dev/null
@@ -0,0 +1,2 @@
+module BitemHelper
+end
diff --git a/app/models/bitem.rb b/app/models/bitem.rb
new file mode 100644 (file)
index 0000000..8dfaf81
--- /dev/null
@@ -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
index 1a86679..901b9a5 100644 (file)
@@ -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 (file)
index 0000000..a3db253
--- /dev/null
@@ -0,0 +1,13 @@
+<%= error_messages_for 'bitem' %>
+
+<!--[form:bitem]-->
+<p><label for="bitem_customer_id">Customer ID</label><br/>
+<%= text_field 'bitem', 'customer_id' %></p>
+
+<p><label for="bitem_merchandise_id">Merchandise ID</label><br/>
+<%= text_field 'bitem', 'merchandise_id' %></p>
+
+<p><label for="bitem_date">Date</label><br/>
+<%= date_select 'bitem', 'date'  %></p>
+<!--[eoform:bitem]-->
+
diff --git a/app/views/bitem/edit.rhtml b/app/views/bitem/edit.rhtml
new file mode 100644 (file)
index 0000000..bb2d7ed
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing bitem</h1>
+
+<% 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 (file)
index 0000000..6b5bb14
--- /dev/null
@@ -0,0 +1,31 @@
+<h1>Listing bitems</h1>
+
+<table>
+  <tr>
+  <th>Customer Name</th>
+  <th>Merchandise Name</th>
+  <% for column in Bitem.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for bitem in @bitems %>
+  <tr>
+  <td><%=h bitem.customer.name %></td>
+  <td><%=h bitem.merchandise.name %></td>
+  <% for column in Bitem.content_columns %>
+    <td><%=h bitem.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => bitem %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => bitem %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => bitem }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= 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 %> 
+
+<br />
+
+<%= link_to 'New bitem', :action => 'new' %>
diff --git a/app/views/bitem/new.rhtml b/app/views/bitem/new.rhtml
new file mode 100644 (file)
index 0000000..d2556da
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New bitem</h1>
+
+<% 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 (file)
index 0000000..fd2d14b
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in Bitem.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @bitem.send(column.name) %>
+</p>
+<% 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 (file)
index 0000000..fc00ace
--- /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>Bitem: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
index c8a1ba0..a328f09 100644 (file)
@@ -2,6 +2,7 @@
 
 <table>
   <tr>
+  <th>Merchandise ID</th>
   <% for column in Merchandise.content_columns %>
     <th><%= column.human_name %></th>
   <% end %>
@@ -9,6 +10,7 @@
   
 <% for merchandise in @merchandises %>
   <tr>
+  <td><%=h merchandise.id %></td>
   <% for column in Merchandise.content_columns %>
     <td><%=h merchandise.send(column.name) %></td>
   <% end %>
index 7b507a5..10e50e7 100644 (file)
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 (file)
index 0000000..641c2c9
--- /dev/null
@@ -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
index 73ad8b3..dcfe7d9 100644 (file)
@@ -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 (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/bitem_controller_test.rb b/test/functional/bitem_controller_test.rb
new file mode 100644 (file)
index 0000000..72063e0
--- /dev/null
@@ -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 (file)
index 0000000..966a42b
--- /dev/null
@@ -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