Add merchandise MVC
authorIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 19:13:49 +0000 (11:13 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 19:13:49 +0000 (11:13 -0800)
This adds a MVC which handles all non-rentable merchandise, such as
popcorn, posters, etc.

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
15 files changed:
app/controllers/merchandise_controller.rb [new file with mode: 0644]
app/helpers/merchandise_helper.rb [new file with mode: 0644]
app/models/merchandise.rb [new file with mode: 0644]
app/views/layouts/merchandise.rhtml [new file with mode: 0644]
app/views/merchandise/_form.rhtml [new file with mode: 0644]
app/views/merchandise/edit.rhtml [new file with mode: 0644]
app/views/merchandise/list.rhtml [new file with mode: 0644]
app/views/merchandise/new.rhtml [new file with mode: 0644]
app/views/merchandise/show.rhtml [new file with mode: 0644]
db/development.sqlite3
db/migrate/014_create_merchandises.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/merchandises.yml [new file with mode: 0644]
test/functional/merchandise_controller_test.rb [new file with mode: 0644]
test/unit/merchandise_test.rb [new file with mode: 0644]

diff --git a/app/controllers/merchandise_controller.rb b/app/controllers/merchandise_controller.rb
new file mode 100644 (file)
index 0000000..062a3ef
--- /dev/null
@@ -0,0 +1,51 @@
+class MerchandiseController < 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
+    @merchandise_pages, @merchandises = paginate :merchandises, :per_page => 10
+  end
+
+  def show
+    @merchandise = Merchandise.find(params[:id])
+  end
+
+  def new
+    @merchandise = Merchandise.new
+  end
+
+  def create
+    @merchandise = Merchandise.new(params[:merchandise])
+    if @merchandise.save
+      flash[:notice] = 'Merchandise was successfully created.'
+      redirect_to :action => 'list'
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @merchandise = Merchandise.find(params[:id])
+  end
+
+  def update
+    @merchandise = Merchandise.find(params[:id])
+    if @merchandise.update_attributes(params[:merchandise])
+      flash[:notice] = 'Merchandise was successfully updated.'
+      redirect_to :action => 'show', :id => @merchandise
+    else
+      render :action => 'edit'
+    end
+  end
+
+  def destroy
+    Merchandise.find(params[:id]).destroy
+    redirect_to :action => 'list'
+  end
+end
diff --git a/app/helpers/merchandise_helper.rb b/app/helpers/merchandise_helper.rb
new file mode 100644 (file)
index 0000000..e6bfee2
--- /dev/null
@@ -0,0 +1,2 @@
+module MerchandiseHelper
+end
diff --git a/app/models/merchandise.rb b/app/models/merchandise.rb
new file mode 100644 (file)
index 0000000..463cfd3
--- /dev/null
@@ -0,0 +1,12 @@
+class Merchandise < ActiveRecord::Base
+  validates_presence_of :name
+  validates_numericality_of :quantity
+  validates_numericality_of :price
+
+  protected
+  def validate
+    errors.add(:quantity, "cannot be negative") if quantity < 0
+    errors.add(:price, "cannot be negative") if price < 0
+    errors.add(:price, "cannot be less than $0.01") if price < 0.01
+  end
+end
diff --git a/app/views/layouts/merchandise.rhtml b/app/views/layouts/merchandise.rhtml
new file mode 100644 (file)
index 0000000..e6a6a3f
--- /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>Merchandise: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
diff --git a/app/views/merchandise/_form.rhtml b/app/views/merchandise/_form.rhtml
new file mode 100644 (file)
index 0000000..d7f7735
--- /dev/null
@@ -0,0 +1,13 @@
+<%= error_messages_for 'merchandise' %>
+
+<!--[form:merchandise]-->
+<p><label for="merchandise_name">Name</label><br/>
+<%= text_field 'merchandise', 'name'  %></p>
+
+<p><label for="merchandise_quantity">Quantity</label><br/>
+<%= text_field 'merchandise', 'quantity'  %></p>
+
+<p><label for="merchandise_price">Price</label><br/>
+<%= text_field 'merchandise', 'price'  %></p>
+<!--[eoform:merchandise]-->
+
diff --git a/app/views/merchandise/edit.rhtml b/app/views/merchandise/edit.rhtml
new file mode 100644 (file)
index 0000000..7efb9d0
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Editing merchandise</h1>
+
+<% form_tag :action => 'update', :id => @merchandise do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @merchandise %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/merchandise/list.rhtml b/app/views/merchandise/list.rhtml
new file mode 100644 (file)
index 0000000..c8a1ba0
--- /dev/null
@@ -0,0 +1,27 @@
+<h1>Listing merchandises</h1>
+
+<table>
+  <tr>
+  <% for column in Merchandise.content_columns %>
+    <th><%= column.human_name %></th>
+  <% end %>
+  </tr>
+  
+<% for merchandise in @merchandises %>
+  <tr>
+  <% for column in Merchandise.content_columns %>
+    <td><%=h merchandise.send(column.name) %></td>
+  <% end %>
+    <td><%= link_to 'Show', :action => 'show', :id => merchandise %></td>
+    <td><%= link_to 'Edit', :action => 'edit', :id => merchandise %></td>
+    <td><%= link_to 'Destroy', { :action => 'destroy', :id => merchandise }, :confirm => 'Are you sure?', :method => :post %></td>
+  </tr>
+<% end %>
+</table>
+
+<%= link_to 'Previous page', { :page => @merchandise_pages.current.previous } if @merchandise_pages.current.previous %>
+<%= link_to 'Next page', { :page => @merchandise_pages.current.next } if @merchandise_pages.current.next %> 
+
+<br />
+
+<%= link_to 'New merchandise', :action => 'new' %>
diff --git a/app/views/merchandise/new.rhtml b/app/views/merchandise/new.rhtml
new file mode 100644 (file)
index 0000000..f03a8fa
--- /dev/null
@@ -0,0 +1,8 @@
+<h1>New merchandise</h1>
+
+<% form_tag :action => 'create' do %>
+  <%= render :partial => 'form' %>
+  <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/merchandise/show.rhtml b/app/views/merchandise/show.rhtml
new file mode 100644 (file)
index 0000000..9c90624
--- /dev/null
@@ -0,0 +1,8 @@
+<% for column in Merchandise.content_columns %>
+<p>
+  <b><%= column.human_name %>:</b> <%=h @merchandise.send(column.name) %>
+</p>
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @merchandise %> |
+<%= link_to 'Back', :action => 'list' %>
index ce0b240..7b507a5 100644 (file)
Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ
diff --git a/db/migrate/014_create_merchandises.rb b/db/migrate/014_create_merchandises.rb
new file mode 100644 (file)
index 0000000..42111ec
--- /dev/null
@@ -0,0 +1,13 @@
+class CreateMerchandises < ActiveRecord::Migration
+  def self.up
+    create_table :merchandises do |t|
+      t.column :name, :string, :null => false
+      t.column :quantity, :integer, :null => false, :default => 0
+      t.column :price, :decimal, :precision => 8, :scale =>2, :default => 0
+    end
+  end
+
+  def self.down
+    drop_table :merchandises
+  end
+end
index 77281f8..73ad8b3 100644 (file)
@@ -2,7 +2,7 @@
 # migrations feature of ActiveRecord to incrementally modify your database, and
 # then regenerate this schema definition.
 
-ActiveRecord::Schema.define(:version => 13) do
+ActiveRecord::Schema.define(:version => 14) do
 
   create_table "coitems", :force => true do |t|
     t.column "customer_id", :integer
@@ -23,6 +23,12 @@ ActiveRecord::Schema.define(:version => 13) do
     t.column "name", :string, :null => false
   end
 
+  create_table "merchandises", :force => true do |t|
+    t.column "name",     :string,                                                 :null => false
+    t.column "quantity", :integer,                               :default => 0,   :null => false
+    t.column "price",    :decimal, :precision => 8, :scale => 2, :default => 0.0
+  end
+
   create_table "rentables", :force => true do |t|
     t.column "type",        :string
     t.column "title",       :string
diff --git a/test/fixtures/merchandises.yml b/test/fixtures/merchandises.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/merchandise_controller_test.rb b/test/functional/merchandise_controller_test.rb
new file mode 100644 (file)
index 0000000..0d58049
--- /dev/null
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'merchandise_controller'
+
+# Re-raise errors caught by the controller.
+class MerchandiseController; def rescue_action(e) raise e end; end
+
+class MerchandiseControllerTest < Test::Unit::TestCase
+  fixtures :merchandises
+
+  def setup
+    @controller = MerchandiseController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+
+    @first_id = merchandises(: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(:merchandises)
+  end
+
+  def test_show
+    get :show, :id => @first_id
+
+    assert_response :success
+    assert_template 'show'
+
+    assert_not_nil assigns(:merchandise)
+    assert assigns(:merchandise).valid?
+  end
+
+  def test_new
+    get :new
+
+    assert_response :success
+    assert_template 'new'
+
+    assert_not_nil assigns(:merchandise)
+  end
+
+  def test_create
+    num_merchandises = Merchandise.count
+
+    post :create, :merchandise => {}
+
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_equal num_merchandises + 1, Merchandise.count
+  end
+
+  def test_edit
+    get :edit, :id => @first_id
+
+    assert_response :success
+    assert_template 'edit'
+
+    assert_not_nil assigns(:merchandise)
+    assert assigns(:merchandise).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 {
+      Merchandise.find(@first_id)
+    }
+
+    post :destroy, :id => @first_id
+    assert_response :redirect
+    assert_redirected_to :action => 'list'
+
+    assert_raise(ActiveRecord::RecordNotFound) {
+      Merchandise.find(@first_id)
+    }
+  end
+end
diff --git a/test/unit/merchandise_test.rb b/test/unit/merchandise_test.rb
new file mode 100644 (file)
index 0000000..efba16f
--- /dev/null
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class MerchandiseTest < Test::Unit::TestCase
+  fixtures :merchandises
+
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end