Skip to content

Commit

Permalink
10.1 イテレーションE1 line_item モデル
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanny committed Jan 22, 2010
1 parent 2d43b49 commit b97a149
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/line_items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class LineItemsController < ApplicationController
# GET /line_items
# GET /line_items.xml
def index
@line_items = LineItem.find(:all)

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

# GET /line_items/1
# GET /line_items/1.xml
def show
@line_item = LineItem.find(params[:id])

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

# GET /line_items/new
# GET /line_items/new.xml
def new
@line_item = LineItem.new

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

# GET /line_items/1/edit
def edit
@line_item = LineItem.find(params[:id])
end

# POST /line_items
# POST /line_items.xml
def create
@line_item = LineItem.new(params[:line_item])

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

# PUT /line_items/1
# PUT /line_items/1.xml
def update
@line_item = LineItem.find(params[:id])

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

# DELETE /line_items/1
# DELETE /line_items/1.xml
def destroy
@line_item = LineItem.find(params[:id])
@line_item.destroy

respond_to do |format|
format.html { redirect_to(line_items_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions depot/app/helpers/line_items_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LineItemsHelper
end
2 changes: 2 additions & 0 deletions depot/app/models/line_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class LineItem < ActiveRecord::Base
end
17 changes: 17 additions & 0 deletions depot/app/views/layouts/line_items.html.erb
Original file line number Diff line number Diff line change
@@ -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>LineItems: <%= 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/line_items/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Editing line_item</h1>

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

<p>
<%= f.label :product_id %><br />
<%= f.text_field :product_id %>
</p>
<p>
<%= f.label :order_id %><br />
<%= f.text_field :order_id %>
</p>
<p>
<%= f.label :quantity %><br />
<%= f.text_field :quantity %>
</p>
<p>
<%= f.label :total_price %><br />
<%= f.text_field :total_price %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @line_item %> |
<%= link_to 'Back', line_items_path %>
26 changes: 26 additions & 0 deletions depot/app/views/line_items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Listing line_items</h1>

<table>
<tr>
<th>Product</th>
<th>Order</th>
<th>Quantity</th>
<th>Total price</th>
</tr>

<% for line_item in @line_items %>
<tr>
<td><%=h line_item.product_id %></td>
<td><%=h line_item.order_id %></td>
<td><%=h line_item.quantity %></td>
<td><%=h line_item.total_price %></td>
<td><%= link_to 'Show', line_item %></td>
<td><%= link_to 'Edit', edit_line_item_path(line_item) %></td>
<td><%= link_to 'Destroy', line_item, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New line_item', new_line_item_path %>
27 changes: 27 additions & 0 deletions depot/app/views/line_items/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>New line_item</h1>

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

<p>
<%= f.label :product_id %><br />
<%= f.text_field :product_id %>
</p>
<p>
<%= f.label :order_id %><br />
<%= f.text_field :order_id %>
</p>
<p>
<%= f.label :quantity %><br />
<%= f.text_field :quantity %>
</p>
<p>
<%= f.label :total_price %><br />
<%= f.text_field :total_price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', line_items_path %>
23 changes: 23 additions & 0 deletions depot/app/views/line_items/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p>
<b>Product:</b>
<%=h @line_item.product_id %>
</p>

<p>
<b>Order:</b>
<%=h @line_item.order_id %>
</p>

<p>
<b>Quantity:</b>
<%=h @line_item.quantity %>
</p>

<p>
<b>Total price:</b>
<%=h @line_item.total_price %>
</p>


<%= link_to 'Edit', edit_line_item_path(@line_item) %> |
<%= link_to 'Back', line_items_path %>
2 changes: 2 additions & 0 deletions depot/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :line_items

map.resources :orders

map.resources :products
Expand Down
16 changes: 16 additions & 0 deletions depot/db/migrate/20100122061534_create_line_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateLineItems < ActiveRecord::Migration
def self.up
create_table :line_items do |t|
t.integer :product_id
t.integer :order_id
t.integer :quantity
t.decimal :total_price

t.timestamps
end
end

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

one:
product_id: 1
order_id: 1
quantity: 1
total_price: 9.99

two:
product_id: 1
order_id: 1
quantity: 1
total_price: 9.99
45 changes: 45 additions & 0 deletions depot/test/functional/line_items_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

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

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

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

assert_redirected_to line_item_path(assigns(:line_item))
end

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

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

test "should update line_item" do
put :update, :id => line_items(:one).id, :line_item => { }
assert_redirected_to line_item_path(assigns(:line_item))
end

test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete :destroy, :id => line_items(:one).id
end

assert_redirected_to line_items_path
end
end
8 changes: 8 additions & 0 deletions depot/test/unit/line_item_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

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

0 comments on commit b97a149

Please sign in to comment.