The Depot Application
11.1 Iteration F1: Adding Users
9.5 Iteration D5: Degrading If Javascript Is Disabled
10.1 Iteration E1: Capturing an Order
rails generate scaffold order name:string address:text email:string pay_type:string
invoke active_record
create db/migrate/20120629191231_create_orders.rb
create app/models/order.rb
invoke test_unit
create test/unit/order_test.rb
create test/fixtures/orders.yml
route resources :orders
invoke scaffold_controller
create app/controllers/orders_controller.rb
invoke erb
create app/views/orders
create app/views/orders/index.html.erb
create app/views/orders/edit.html.erb
create app/views/orders/show.html.erb
create app/views/orders/new.html.erb
create app/views/orders/_form.html.erb
invoke test_unit
create test/functional/orders_controller_test.rb
invoke helper
create app/helpers/orders_helper.rb
invoke test_unit
create test/unit/helpers/orders_helper_test.rb
invoke stylesheets
identical public/stylesheets/scaffold.css
rails generate scaffold line_item product_id:integer order_id:integer quantity:integer total_price:decimal
invoke active_record
create db/migrate/20120629191235_create_line_items.rb
create app/models/line_item.rb
invoke test_unit
create test/unit/line_item_test.rb
create test/fixtures/line_items.yml
route resources :line_items
invoke scaffold_controller
create app/controllers/line_items_controller.rb
invoke erb
create app/views/line_items
create app/views/line_items/index.html.erb
create app/views/line_items/edit.html.erb
create app/views/line_items/show.html.erb
create app/views/line_items/new.html.erb
create app/views/line_items/_form.html.erb
invoke test_unit
create test/functional/line_items_controller_test.rb
invoke helper
create app/helpers/line_items_helper.rb
invoke test_unit
create test/unit/helpers/line_items_helper_test.rb
invoke stylesheets
identical public/stylesheets/scaffold.css
edit db/migrate/20120629191231_create_orders.rb
def self.up
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type, :limit => 10
t.timestamps
end
end
edit db/migrate/20120629191235_create_line_items.rb
class CreateLineItems < ActiveRecord::Migration
def self.up
create_table :line_items do |t|
t.integer :product_id, :null => false, :options =>
"CONSTRAINT fk_line_item_products REFERENCES products(id)"
t.integer :order_id, :null => false, :options =>
"CONSTRAINT fk_line_item_orders REFERENCES orders(id)"
t.integer :quantity, :null => false
t.decimal :total_price, :null => false, :precision => 8, :scale => 2
t.timestamps
end
end
def self.down
drop_table :line_items
end
end
rake db:migrate
mv 20120629191231_create_orders.rb 20110711000005_create_orders.rb
mv 20120629191235_create_line_items.rb 20110711000006_create_line_items.rb
== CreateOrders: migrating ===================================================
-- create_table(:orders)
-> 0.0019s
== CreateOrders: migrated (0.0021s) ==========================================
== CreateLineItems: migrating ================================================
-- create_table(:line_items)
-> 0.0035s
== CreateLineItems: migrated (0.0037s) =======================================
sqlite3 db/development.sqlite3 .schema
CREATE TABLE "line_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "product_id" integer NOT NULL, "order_id" integer NOT NULL, "quantity" integer NOT NULL, "total_price" decimal(8,2) NOT NULL, "created_at" datetime, "updated_at" datetime);
CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" text, "email" varchar(255), "pay_type" varchar(10), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "image_url" varchar(255), "created_at" datetime, "updated_at" datetime, "price" decimal(8,2) DEFAULT 0);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE TABLE "sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id" varchar(255) NOT NULL, "data" text, "created_at" datetime, "updated_at" datetime);
CREATE INDEX "index_sessions_on_session_id" ON "sessions" ("session_id");
CREATE INDEX "index_sessions_on_updated_at" ON "sessions" ("updated_at");
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
edit app/models/order.rb
class Order < ActiveRecord::Base
has_many :line_items
end
edit app/models/product.rb
class Product < ActiveRecord::Base
has_many :line_items
# ...
edit app/models/line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
edit app/views/store/_cart.html.erb
<div class="cart-title">Your Cart</div>
<table>
<%= render(:partial => "cart_item", :collection => cart.items) %>
<tr class="total-line">
<td colspan="2">Total</td>
<td class="total-cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to "Checkout", :action => 'checkout' %>
<%= button_to "Empty cart", :action => :empty_cart %>
edit app/controllers/store_controller.rb
def checkout
@cart = find_cart
if @cart.items.empty?
redirect_to_index("Your cart is empty")
else
@order = Order.new
end
end
edit app/views/store/checkout.html.erb
<div class="depot-form">
<% if @order.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for :order, :url => { :action => :save_order } do |form| %>
<fieldset>
<legend>Please Enter Your Details</legend>
<div>
<%= form.label :name, "Name:" %>
<%= form.text_field :name, :size => 40 %>
</div>
<div>
<%= form.label :address, "Address:" %>
<%= form.text_area :address, :rows => 3, :cols => 40 %>
</div>
<div>
<%= form.label :email, "E-Mail:" %>
<%= form.text_field :email, :size => 40 %>
</div>
<div>
<%= form.label :pay_type, "Pay with:" %>
<%=
form.select :pay_type,
Order::PAYMENT_TYPES,
:prompt => "Select a payment method"
%>
</div>
<%= submit_tag "Place Order", :class => "submit" %>
</fieldset>
<% end %>
</div>
edit app/models/order.rb
class Order < ActiveRecord::Base
PAYMENT_TYPES = [
# Displayed stored in db
[ "Check", "check" ],
[ "Credit card", "cc" ],
[ "Purchase order", "po" ]
]
# ...
edit public/stylesheets/depot.css
/* Styles for order form */
.depot-form fieldset {
background: #efe;
}
.depot-form legend {
color: #dfd;
background: #141;
font-family: sans-serif;
padding: 0.2em 1em;
}
.depot-form label {
width: 5em;
float: left;
text-align: right;
padding-top: 0.2em;
margin-right: 0.1em;
display: block;
}
.depot-form select, .depot-form textarea, .depot-form input {
margin-left: 0.5em;
}
.depot-form .submit {
margin-left: 4em;
}
.depot-form div {
margin: 0.5em 0;
}
get /store/checkout
Pragmatic Bookshelf
get /store/save_order
Unknown action
The action 'save_order' could not be found for StoreController
edit app/models/order.rb
class Order < ActiveRecord::Base
PAYMENT_TYPES = [
# Displayed stored in db
[ "Check", "check" ],
[ "Credit card", "cc" ],
[ "Purchase order", "po" ]
]
validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in =>
PAYMENT_TYPES.map {|disp, value| value}
# ...
edit app/controllers/store_controller.rb
def save_order
@cart = find_cart
@order = Order.new(params[:order]) # <label id="code.p.new.order"/>
@order.add_line_items_from_cart(@cart) # <label id="code.p.append.li"/>
if @order.save # <label id="code.p.save"/>
session[:cart] = nil
redirect_to_index("Thank you for your order")
else
render :action => 'checkout'
end
end
edit app/models/order.rb
class Order < ActiveRecord::Base
PAYMENT_TYPES = [
# Displayed stored in db
[ "Check", "check" ],
[ "Credit card", "cc" ],
[ "Purchase order", "po" ]
]
# ...
validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in =>
PAYMENT_TYPES.map {|disp, value| value}
# ...
has_many :line_items
def add_line_items_from_cart(cart)
cart.items.each do |item|
li = LineItem.from_cart_item(item)
line_items << li
end
end
end
edit app/models/line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
def self.from_cart_item(cart_item)
li = self.new
li.product = cart_item.product
li.quantity = cart_item.quantity
li.total_price = cart_item.price
li
end
end
sqlite3> select * from orders
sqlite3> select * from line_items
get /store/save_order
Pragmatic Bookshelf
get /store/checkout
Pragmatic Bookshelf
post /store/save_order
order[pay_type] => check
order[address] => 123 Main St
order[email] => customer@example.com
order[name] => Dave Thomas
You are being
redirected .
get http://localhost:3000/store/index
Pragmatic Bookshelf
Thank you for your order
Your Pragmatic Catalog
Pragmatic Project Automation
Pragmatic Project Automation shows you how to improve the
consistency and repeatability of your project's procedures using
automation to reduce risk and errors.
Simply put, we're going to put this thing called a computer to work
for you doing the mundane (but important) project stuff. That means
you'll have more time and energy to do the really
exciting---and difficult---stuff, like writing quality code.
Pragmatic Unit Testing (C#)
Pragmatic programmers use feedback to drive their development and
personal processes. The most valuable feedback you can get while
coding comes from unit testing.
Without good tests in place, coding can become a frustrating game of
"whack-a-mole." That's the carnival game where the player strikes at a
mechanical mole; it retreats and another mole pops up on the opposite side
of the field. The moles pop up and down so fast that you end up flailing
your mallet helplessly as the moles continue to pop up where you least
expect them.
Pragmatic Version Control
This book is a recipe-based approach to using Subversion that will
get you up and running quickly---and correctly. All projects need
version control: it's a foundational piece of any project's
infrastructure. Yet half of all project teams in the U.S. don't use
any version control at all. Many others don't use it well, and end
up experiencing time-consuming problems.
sqlite3> select * from orders
id = 1
name = Dave Thomas
address = 123 Main St
email = customer@example.com
pay_type = check
created_at = 2012-06-29 19:12:46.938145
updated_at = 2012-06-29 19:12:46.938145
sqlite3> select * from line_items
id = 1
product_id = 3
order_id = 1
quantity = 1
total_price = 28.5
created_at = 2012-06-29 19:12:46.942016
updated_at = 2012-06-29 19:12:46.942016
edit app/views/store/add_to_cart.js.rjs
page.select("div#notice").each { |div| div.hide }
page.replace_html("cart", :partial => "cart", :object => @cart)
page[:cart].visual_effect :blind_down if @cart.total_items == 1
page[:current_item].visual_effect :highlight,
:startcolor => "#88ff88",
:endcolor => "#114411"
11.1 Iteration F1: Adding Users
9.5 Iteration D5: Degrading If Javascript Is Disabled