Skip to content

Commit

Permalink
10.1 イテレーションE1 order モデル
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanny committed Jan 22, 2010
1 parent 6ebf5f9 commit 2d43b49
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 0 deletions.
85 changes: 85 additions & 0 deletions depot/app/controllers/orders_controller.rb
@@ -0,0 +1,85 @@
class OrdersController < ApplicationController
# GET /orders
# GET /orders.xml
def index
@orders = Order.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @orders }
end
end

# GET /orders/1
# GET /orders/1.xml
def show
@order = Order.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @order }
end
end

# GET /orders/new
# GET /orders/new.xml
def new
@order = Order.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @order }
end
end

# GET /orders/1/edit
def edit
@order = Order.find(params[:id])
end

# POST /orders
# POST /orders.xml
def create
@order = Order.new(params[:order])

respond_to do |format|
if @order.save
flash[:notice] = 'Order was successfully created.'
format.html { redirect_to(@order) }
format.xml { render :xml => @order, :status => :created, :location => @order }
else
format.html { render :action => "new" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
end

# PUT /orders/1
# PUT /orders/1.xml
def update
@order = Order.find(params[:id])

respond_to do |format|
if @order.update_attributes(params[:order])
flash[:notice] = 'Order was successfully updated.'
format.html { redirect_to(@order) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /orders/1
# DELETE /orders/1.xml
def destroy
@order = Order.find(params[:id])
@order.destroy

respond_to do |format|
format.html { redirect_to(orders_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions depot/app/helpers/orders_helper.rb
@@ -0,0 +1,2 @@
module OrdersHelper
end
2 changes: 2 additions & 0 deletions depot/app/models/order.rb
@@ -0,0 +1,2 @@
class Order < ActiveRecord::Base
end
17 changes: 17 additions & 0 deletions depot/app/views/layouts/orders.html.erb
@@ -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>Orders: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield %>

</body>
</html>
28 changes: 28 additions & 0 deletions depot/app/views/orders/edit.html.erb
@@ -0,0 +1,28 @@
<h1>Editing order</h1>

<% form_for(@order) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :pay_type %><br />
<%= f.text_field :pay_type %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @order %> |
<%= link_to 'Back', orders_path %>
26 changes: 26 additions & 0 deletions depot/app/views/orders/index.html.erb
@@ -0,0 +1,26 @@
<h1>Listing orders</h1>

<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Pay type</th>
</tr>

<% for order in @orders %>
<tr>
<td><%=h order.name %></td>
<td><%=h order.address %></td>
<td><%=h order.email %></td>
<td><%=h order.pay_type %></td>
<td><%= link_to 'Show', order %></td>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<td><%= link_to 'Destroy', order, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New order', new_order_path %>
27 changes: 27 additions & 0 deletions depot/app/views/orders/new.html.erb
@@ -0,0 +1,27 @@
<h1>New order</h1>

<% form_for(@order) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :pay_type %><br />
<%= f.text_field :pay_type %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', orders_path %>
23 changes: 23 additions & 0 deletions depot/app/views/orders/show.html.erb
@@ -0,0 +1,23 @@
<p>
<b>Name:</b>
<%=h @order.name %>
</p>

<p>
<b>Address:</b>
<%=h @order.address %>
</p>

<p>
<b>Email:</b>
<%=h @order.email %>
</p>

<p>
<b>Pay type:</b>
<%=h @order.pay_type %>
</p>


<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>
2 changes: 2 additions & 0 deletions depot/config/routes.rb
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :orders

map.resources :products

# The priority is based upon order of creation: first created -> highest priority.
Expand Down
16 changes: 16 additions & 0 deletions depot/db/migrate/20100122061403_create_orders.rb
@@ -0,0 +1,16 @@
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type

t.timestamps
end
end

def self.down
drop_table :orders
end
end
13 changes: 13 additions & 0 deletions depot/test/fixtures/orders.yml
@@ -0,0 +1,13 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString
address: MyText
email: MyString
pay_type: MyString

two:
name: MyString
address: MyText
email: MyString
pay_type: MyString
45 changes: 45 additions & 0 deletions depot/test/functional/orders_controller_test.rb
@@ -0,0 +1,45 @@
require 'test_helper'

class OrdersControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:orders)
end

test "should get new" do
get :new
assert_response :success
end

test "should create order" do
assert_difference('Order.count') do
post :create, :order => { }
end

assert_redirected_to order_path(assigns(:order))
end

test "should show order" do
get :show, :id => orders(:one).id
assert_response :success
end

test "should get edit" do
get :edit, :id => orders(:one).id
assert_response :success
end

test "should update order" do
put :update, :id => orders(:one).id, :order => { }
assert_redirected_to order_path(assigns(:order))
end

test "should destroy order" do
assert_difference('Order.count', -1) do
delete :destroy, :id => orders(:one).id
end

assert_redirected_to orders_path
end
end
8 changes: 8 additions & 0 deletions depot/test/unit/order_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class OrderTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit 2d43b49

Please sign in to comment.