Agile Web Development with Rails, Edition 4

13.1 Iteration H1: Email Notifications 12.3 Iteration G3: Downloading an eBook

12.4 Playtime

39 (tests|runs), 94 assertions, 0 failures, 0 errors.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:38:in `assert_test_summary'
  /home/rubys/git/awdwr/edition4/checkdepot.rb:336:in `block in <class:DepotTest>'

Add the xml format to the controller

edit app/controllers/products_controller.rb
  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.xml { render :xml => @product }
        format.atom
      end
    end
  end

Fetch the XML, see that there are no orders there

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.atom
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:localhost,2005:/products/2/who_bought</id>
  <link rel="alternate" type="text/html" href="http://localhost:3000"/>
  <link rel="self" type="application/atom+xml" href="http://localhost:3000/products/2/who_bought.atom"/>
  <title>Who bought Rails, Angular, Postgres, and Bootstrap</title>
  <updated>2016-06-13T19:41:15Z</updated>
  <entry>
    <id>tag:localhost,2005:Order/1</id>
    <published>2016-06-13T19:41:13Z</published>
    <updated>2016-06-13T19:41:13Z</updated>
    <link rel="alternate" type="text/html" href="http://localhost:3000/orders/1"/>
    <title>Order 1</title>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Shipped to 123 Main St</p>
        <table>
          <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Total Price</th>
          </tr>
          <tr>
            <td>Rails, Angular, Postgres, and Bootstrap</td>
            <td>1</td>
            <td>$45.00</td>
          </tr>
          <tr>
            <th colspan="2">total</th>
            <th>$45.00</th>
          </tr>
        </table>
        <p>Paid by Check</p>
      </div>
    </summary>
    <author>
      <name>Dave Thomas</name>
      <email>customer@example.com</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2005:Order/2</id>
    <published>2016-06-13T19:41:15Z</published>
    <updated>2016-06-13T19:41:15Z</updated>
    <link rel="alternate" type="text/html" href="http://localhost:3000/orders/2"/>
    <title>Order 2</title>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Shipped to 123 Main St</p>
        <table>
          <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Total Price</th>
          </tr>
          <tr>
            <td>Rails, Angular, Postgres, and Bootstrap</td>
            <td>1</td>
            <td>$45.00</td>
          </tr>
          <tr>
            <th colspan="2">total</th>
            <th>$45.00</th>
          </tr>
        </table>
        <p>Paid by Check</p>
      </div>
    </summary>
    <author>
      <name>Dave Thomas</name>
      <email>customer@example.com</email>
    </author>
  </entry>
</feed>

Include "orders" in the response

edit app/controllers/products_controller.rb
  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.xml { render xml: @product.to_xml(include: :orders) }
        format.atom
      end
    end
  end

Fetch the xml, see that the orders are included

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
<?xml version="1.0" encoding="UTF-8"?>
<product>
  <id type="integer">2</id>
  <title>Rails, Angular, Postgres, and Bootstrap</title>
  <description>&lt;p&gt;
      &lt;em&gt;Powerful, Effective, and Efficient Full-Stack Web Development&lt;/em&gt;
      As a Rails developer, you care about user experience and performance,
      but you also want simple and maintainable code. Achieve all that by
      embracing the full stack of web development, from styling with
      Bootstrap, building an interactive user interface with AngularJS, to
      storing data quickly and reliably in PostgreSQL. Take a holistic view of
      full-stack development to create usable, high-performing applications,
      and learn to use these technologies effectively in a Ruby on Rails
      environment.
      &lt;/p&gt;</description>
  <image-url>dcbang.jpg</image-url>
  <price type="decimal">45.0</price>
  <created-at type="dateTime">2016-06-13T19:40:34Z</created-at>
  <updated-at type="dateTime">2016-06-13T19:40:34Z</updated-at>
  <orders type="array">
    <order>
      <id type="integer">1</id>
      <name>Dave Thomas</name>
      <address>123 Main St</address>
      <email>customer@example.com</email>
      <pay-type>Check</pay-type>
      <created-at type="dateTime">2016-06-13T19:41:13Z</created-at>
      <updated-at type="dateTime">2016-06-13T19:41:13Z</updated-at>
    </order>
    <order>
      <id type="integer">2</id>
      <name>Dave Thomas</name>
      <address>123 Main St</address>
      <email>customer@example.com</email>
      <pay-type>Check</pay-type>
      <created-at type="dateTime">2016-06-13T19:41:15Z</created-at>
      <updated-at type="dateTime">2016-06-13T19:41:15Z</updated-at>
    </order>
  </orders>
</product>

Define an HTML view

edit app/views/products/who_bought.html.erb
<h3>People Who Bought <%= @product.title %></h3>
 
<ul>
  <% for order in @product.orders %>
    <li>
      <%= mail_to order.email, order.name %>
    </li>
  <% end %>
</ul>

Add the html format to the controller

edit app/controllers/products_controller.rb
  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.html
        format.xml { render xml: @product.to_xml(include: :orders) }
        format.atom
      end
    end
  end

See the (raw) HTML

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought
<!DOCTYPE html>
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <link data-turbolinks-track="reload" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/carts.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/line_items.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/orders.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/products.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="reload" href="/assets/store.css?body=1" media="all" rel="stylesheet" />
  <script data-turbolinks-track="reload" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/jquery-ui/effect.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/jquery-ui/effect-blind.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/carts.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/line_items.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/orders.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/products.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/store.js?body=1"></script>
<script data-turbolinks-track="reload" src="/assets/application.js?body=1"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="vIimnEfXS9b067iLXbOhfQkrOdxqqv4l4zrN/vv699Y=" name="csrf-token" />
</head>
<body class="products">
  <div id="banner">
    <img alt="The Pragmatic Bookshelf" src="/assets/logo.svg" />
    <span class="title"></span>
  </div>
  <div id="columns">
    <div id="side">
<!-- START_HIGHLIGHT -->
 
      <!-- END_HIGHLIGHT -->
 
      <ul>
        <li><a href="http://www....">Home</a></li>
        <li><a href="http://www..../faq">Questions</a></li>
        <li><a href="http://www..../news">News</a></li>
        <li><a href="http://www..../contact">Contact</a></li>
      </ul>
    </div>
    <div id="main">
      <h3>People Who Bought Rails, Angular, Postgres, and Bootstrap</h3>
 
<ul>
    <li>
      <a href="mailto:customer@example.com">Dave Thomas</a>
    </li>
    <li>
      <a href="mailto:customer@example.com">Dave Thomas</a>
    </li>
</ul>
 
    </div>
  </div>
</body>
</html>

Anything that XML can do, JSON can too...

edit app/controllers/products_controller.rb
  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.html
        format.xml { render xml: @product.to_xml(include: :orders) }
        format.atom
        format.json { render json: @product.to_json(include: :orders) }
      end
    end
  end

Fetch the data in JSON format

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.json
{"id":2,"title":"Rails, Angular, Postgres, and Bootstrap","description":"\u003Cp\u003E\n      \u003Cem\u003EPowerful, Effective, and Efficient Full-Stack Web Development\u003C/em\u003E\n      As a Rails developer, you care about user experience and performance,\n      but you also want simple and maintainable code. Achieve all that by\n      embracing the full stack of web development, from styling with\n      Bootstrap, building an interactive user interface with AngularJS, to\n      storing data quickly and reliably in PostgreSQL. Take a holistic view of\n      full-stack development to create usable, high-performing applications,\n      and learn to use these technologies effectively in a Ruby on Rails\n      environment.\n      \u003C/p\u003E","image_url":"dcbang.jpg","price":"45.0","created_at":"2016-06-13T19:40:34.175Z","updated_at":"2016-06-13T19:40:34.175Z","orders":[{"id":1,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2016-06-13T19:41:13.391Z","updated_at":"2016-06-13T19:41:13.391Z"},{"id":2,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2016-06-13T19:41:15.221Z","updated_at":"2016-06-13T19:41:15.221Z"}]}

Customize the xml

edit app/views/products/who_bought.xml.builder
xml.order_list(:for_product => @product.title) do
  for o in @product.orders
    xml.order do
      xml.name(o.name)
      xml.email(o.email)
    end
  end
end

Change the rendering to use templates

edit app/controllers/products_controller.rb
  def who_bought
    @product = Product.find(params[:id])
    @latest_order = @product.orders.order(:updated_at).last
    if stale?(@latest_order)
      respond_to do |format|
        format.html
        format.xml
        format.atom
        format.json { render json: @product.to_json(include: :orders) }
      end
    end
  end

Fetch the (much streamlined) XML

curl --max-time 15 --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
<order_list for_product="Rails, Angular, Postgres, and Bootstrap">
  <order>
    <name>Dave Thomas</name>
    <email>customer@example.com</email>
  </order>
  <order>
    <name>Dave Thomas</name>
    <email>customer@example.com</email>
  </order>
</order_list>

Verify that the tests still pass

rake test
rake aborted!
ActiveRecord::PendingMigrationError: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=test' to resolve this issue.
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:383:in `check_pending!'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:6:in `<class:TestCase>'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:5:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/helpers/carts_helper_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (3 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (2 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `block in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:61:in `block in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/gems/rake-11.2.1/exe/rake:27:in `<top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:run => test:units
(See full trace by running task with --trace)
Run options: --seed 12752
 
# Running tests:
 
 
 
Finished tests in 0.000770s, 0.0000 tests/s, 0.0000 assertions/s.
 
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Commit

git commit -a -m "Orders"
[master fa5c036] Orders
 9 files changed, 103 insertions(+), 41 deletions(-)
git tag iteration-g

13.1 Iteration H1: Email Notifications 12.3 Iteration G3: Downloading an eBook