changelog shortlog tags manifest raw

changeset: add scaffold product.

changeset 9: 5cf82beef889
parent 8:eddc0740bd25
child 10:c7be8325050f
author: moriq@moriq.com
date: Mon Mar 10 03:22:11 2008 +0900 (16 years ago)
files: app/controllers/products_controller.rb app/helpers/products_helper.rb app/models/product.rb app/views/layouts/products.html.erb app/views/products/edit.html.erb app/views/products/index.html.erb app/views/products/new.html.erb app/views/products/show.html.erb config/routes.rb db/migrate/002_create_products.rb public/stylesheets/scaffold.css test/fixtures/products.yml test/functional/products_controller_test.rb test/unit/product_test.rb
description: add scaffold product.
--- a/config/routes.rb	Wed Mar 05 04:31:05 2008 +0900
+++ b/config/routes.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw d
 ActionController::Routing::Routes.draw do |map|
+  map.resources :products
+
   map.resources :users
 
   map.resource :session
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controllers/products_controller.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,85 @@
+class ProductsController < ApplicationController
+  # GET /products
+  # GET /products.xml
+  def index
+    @products = Product.find(:all)
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.xml  { render :xml => @products }
+    end
+  end
+
+  # GET /products/1
+  # GET /products/1.xml
+  def show
+    @product = Product.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.xml  { render :xml => @product }
+    end
+  end
+
+  # GET /products/new
+  # GET /products/new.xml
+  def new
+    @product = Product.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.xml  { render :xml => @product }
+    end
+  end
+
+  # GET /products/1/edit
+  def edit
+    @product = Product.find(params[:id])
+  end
+
+  # POST /products
+  # POST /products.xml
+  def create
+    @product = Product.new(params[:product])
+
+    respond_to do |format|
+      if @product.save
+        flash[:notice] = 'Product was successfully created.'
+        format.html { redirect_to(@product) }
+        format.xml  { render :xml => @product, :status => :created, :location => @product }
+      else
+        format.html { render :action => "new" }
+        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /products/1
+  # PUT /products/1.xml
+  def update
+    @product = Product.find(params[:id])
+
+    respond_to do |format|
+      if @product.update_attributes(params[:product])
+        flash[:notice] = 'Product was successfully updated.'
+        format.html { redirect_to(@product) }
+        format.xml  { head :ok }
+      else
+        format.html { render :action => "edit" }
+        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /products/1
+  # DELETE /products/1.xml
+  def destroy
+    @product = Product.find(params[:id])
+    @product.destroy
+
+    respond_to do |format|
+      format.html { redirect_to(products_url) }
+      format.xml  { head :ok }
+    end
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/helpers/products_helper.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/product.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,2 @@
+class Product < ActiveRecord::Base
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/layouts/products.html.erb	Mon Mar 10 03:22:11 2008 +0900
@@ -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>Products: <%= controller.action_name %></title>
+  <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield  %>
+
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/products/edit.html.erb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,22 @@
+<h1>Editing product</h1>
+
+<%= error_messages_for :product %>
+
+<% form_for(@product) do |f| %>
+  <p>
+    <b>Name</b><br />
+    <%= f.text_field :name %>
+  </p>
+
+  <p>
+    <b>Price</b><br />
+    <%= f.text_field :price %>
+  </p>
+
+  <p>
+    <%= f.submit "Update" %>
+  </p>
+<% end %>
+
+<%= link_to 'Show', @product %> |
+<%= link_to 'Back', products_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/products/index.html.erb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,22 @@
+<h1>Listing products</h1>
+
+<table>
+  <tr>
+    <th>Name</th>
+    <th>Price</th>
+  </tr>
+
+<% for product in @products %>
+  <tr>
+    <td><%=h product.name %></td>
+    <td><%=h product.price %></td>
+    <td><%= link_to 'Show', product %></td>
+    <td><%= link_to 'Edit', edit_product_path(product) %></td>
+    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
+  </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New product', new_product_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/products/new.html.erb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,21 @@
+<h1>New product</h1>
+
+<%= error_messages_for :product %>
+
+<% form_for(@product) do |f| %>
+  <p>
+    <b>Name</b><br />
+    <%= f.text_field :name %>
+  </p>
+
+  <p>
+    <b>Price</b><br />
+    <%= f.text_field :price %>
+  </p>
+
+  <p>
+    <%= f.submit "Create" %>
+  </p>
+<% end %>
+
+<%= link_to 'Back', products_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/products/show.html.erb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,13 @@
+<p>
+  <b>Name:</b>
+  <%=h @product.name %>
+</p>
+
+<p>
+  <b>Price:</b>
+  <%=h @product.price %>
+</p>
+
+
+<%= link_to 'Edit', edit_product_path(@product) %> |
+<%= link_to 'Back', products_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db/migrate/002_create_products.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,14 @@
+class CreateProducts < ActiveRecord::Migration
+  def self.up
+    create_table :products do |t|
+      t.string :name
+      t.integer :price
+
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :products
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/public/stylesheets/scaffold.css	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,74 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size:   13px;
+  line-height: 18px;
+}
+
+pre {
+  background-color: #eee;
+  padding: 10px;
+  font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+.fieldWithErrors {
+  padding: 2px;
+  background-color: red;
+  display: table;
+}
+
+#errorExplanation {
+  width: 400px;
+  border: 2px solid red;
+  padding: 7px;
+  padding-bottom: 12px;
+  margin-bottom: 20px;
+  background-color: #f0f0f0;
+}
+
+#errorExplanation h2 {
+  text-align: left;
+  font-weight: bold;
+  padding: 5px 5px 5px 15px;
+  font-size: 12px;
+  margin: -7px;
+  background-color: #c00;
+  color: #fff;
+}
+
+#errorExplanation p {
+  color: #333;
+  margin-bottom: 0;
+  padding: 5px;
+}
+
+#errorExplanation ul li {
+  font-size: 12px;
+  list-style: square;
+}
+
+div.uploadStatus {
+  margin: 5px;
+}
+
+div.progressBar {
+  margin: 5px;
+}
+
+div.progressBar div.border {
+  background-color: #fff;
+  border: 1px solid gray;
+  width: 100%;
+}
+
+div.progressBar div.background {
+  background-color: #333;
+  height: 18px;
+  width: 0%;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/fixtures/products.yml	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+  name: にんじん
+  price: 100
+
+two:
+  name: たまねぎ
+  price: 150
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/functional/products_controller_test.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,45 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ProductsControllerTest < ActionController::TestCase
+  def test_should_get_index
+    get :index
+    assert_response :success
+    assert_not_nil assigns(:products)
+  end
+
+  def test_should_get_new
+    get :new
+    assert_response :success
+  end
+
+  def test_should_create_product
+    assert_difference('Product.count') do
+      post :create, :product => { }
+    end
+
+    assert_redirected_to product_path(assigns(:product))
+  end
+
+  def test_should_show_product
+    get :show, :id => products(:one).id
+    assert_response :success
+  end
+
+  def test_should_get_edit
+    get :edit, :id => products(:one).id
+    assert_response :success
+  end
+
+  def test_should_update_product
+    put :update, :id => products(:one).id, :product => { }
+    assert_redirected_to product_path(assigns(:product))
+  end
+
+  def test_should_destroy_product
+    assert_difference('Product.count', -1) do
+      delete :destroy, :id => products(:one).id
+    end
+
+    assert_redirected_to products_path
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unit/product_test.rb	Mon Mar 10 03:22:11 2008 +0900
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ProductTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  def test_truth
+    assert true
+  end
+end