--- /dev/null
+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
--- /dev/null
+module MerchandiseHelper
+end
--- /dev/null
+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
--- /dev/null
+<!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>
--- /dev/null
+<%= 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]-->
+
--- /dev/null
+<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' %>
--- /dev/null
+<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' %>
--- /dev/null
+<h1>New merchandise</h1>
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
--- /dev/null
+<% 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' %>
--- /dev/null
+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
# 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
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
--- /dev/null
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+two:
+ id: 2
--- /dev/null
+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
--- /dev/null
+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