12.8 Iteration J3: Integration Tests 12.4 Playtime
rails generate mailer Notifier order_received order_shipped
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from /home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
create app/mailers/notifier.rb
invoke erb
create app/views/notifier
create app/views/notifier/order_received.text.erb
create app/views/notifier/order_shipped.text.erb
invoke test_unit
create test/functional/notifier_test.rb
pub depot_p
edit app/mailers/notifier.rb
class Notifier < ActionMailer::Base
default :from => 'Sam Ruby <depot@example.com>'
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.actionmailer.notifier.order_received.subject
#
def order_received(order)
@order = order
mail :to => order.email, :subject => 'Pragmatic Store Order Confirmation'
end
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.actionmailer.notifier.order_shipped.subject
#
def order_shipped(order)
@order = order
mail :to => order.email, :subject => 'Pragmatic Store Order Shipped'
end
end
Tailor the confirm receipt email
edit app/views/notifier/order_received.text.erb
Dear <%= @order.name %>
Thank you for your recent order from The Pragmatic Store.
You ordered the following items:
<%= render @order.line_items -%>
We'll send you a separate e-mail when your order ships.
Text partial for the line items
edit app/views/line_items/_line_item.text.erb
<%= sprintf("%2d x %s",
line_item.quantity,
truncate(line_item.product.title, :length => 50)) %>
HTML partial for the line items
edit app/views/line_items/_line_item.html.erb
<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
Tailor the confirm shipped email
edit app/views/notifier/order_shipped.html.erb
<h3>Pragmatic Order Shipped</h3>
<p>
This is just to let you know that we've shipped your recent order:
</p>
<table>
<tr><th colspan="2">Qty</th><th>Description</th></tr>
<%= render @order.line_items %>
</table>
Update the test case
Not helpful: 'Hi, find me in app'
edit test/functional/notifier_test.rb
require 'test_helper'
class NotifierTest < ActionMailer::TestCase
test "order_received" do
mail = Notifier.order_received(orders(:one))
assert_equal "Pragmatic Store Order Confirmation", mail.subject
assert_equal ["dave@example.org"], mail.to
assert_equal ["depot@example.com"], mail.from
assert_match /1 x Programming Ruby 1.9/, mail.body.encoded
end
test "order_shipped" do
mail = Notifier.order_shipped(orders(:one))
assert_equal "Pragmatic Store Order Shipped", mail.subject
assert_equal ["dave@example.org"], mail.to
assert_equal ["depot@example.com"], mail.from
assert_match /<td>1×<\/td>\s*<td>Programming Ruby 1.9<\/td>/,
mail.body.encoded
end
end
rake db:test:load
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from /home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
(in /home/rubys/svn/rails4/Book/util/work-188/depot)
ruby -I test test/functional/notifier_test.rb
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from /home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)
Loaded suite test/functional/notifier_test
Started
..
Finished in 0.330163 seconds.
2 tests, 8 assertions, 0 failures, 0 errors