Agile Web Development with Rails, Edition 4

14.1 Iteration I1: Email Notifications 12.4 Iteration G2: Downloading an eBook

12.5 Playtime

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>2017-06-03T07:41:33Z</updated>
  <entry>
    <id>tag:localhost,2005:Order/1</id>
    <published>2017-06-03T07:41:31Z</published>
    <updated>2017-06-03T07:41:31Z</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>2017-06-03T07:41:33Z</published>
    <updated>2017-06-03T07:41:33Z</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">2017-06-03T07:40:32Z</created-at>
  <updated-at type="dateTime">2017-06-03T07:40:32Z</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 type="integer">Check</pay-type>
      <created-at type="dateTime">2017-06-03T07:41:31Z</created-at>
      <updated-at type="dateTime">2017-06-03T07:41:31Z</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 type="integer">Check</pay-type>
      <created-at type="dateTime">2017-06-03T07:41:33Z</created-at>
      <updated-at type="dateTime">2017-06-03T07:41:33Z</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>
    <meta content="authenticity_token" name="csrf-param" />
<meta content="YKoez4EC4sPRjaRCY7FHYTIn0uVc4g5EZVXMwAHe4SU=" name="csrf-token" />
 
    <meta content="authenticity_token" name="csrf-param" />
<meta content="YKoez4EC4sPRjaRCY7FHYTIn0uVc4g5EZVXMwAHe4SU=" name="csrf-token" />
 
    <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" />
<link data-turbolinks-track="reload" href="/assets/application.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/version.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/effects/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>
  </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":"2017-06-03T07:40:32.844Z","updated_at":"2017-06-03T07:40:32.844Z","orders":[{"id":1,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-06-03T07:41:31.240Z","updated_at":"2017-06-03T07:41:31.240Z"},{"id":2,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-06-03T07:41:33.271Z","updated_at":"2017-06-03T07:41:33.271Z"}]}

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
Run options: --seed 24140
 
# Running:
 
.......................................
 
Finished in 0.338533s, 115.2028 runs/s, 277.6682 assertions/s.
 
39 runs, 94 assertions, 0 failures, 0 errors, 0 skips

Commit

git commit -a -m "Orders"
[master 9cfc045] Orders
 9 files changed, 101 insertions(+), 44 deletions(-)
git tag iteration-g

14.1 Iteration I1: Email Notifications 12.4 Iteration G2: Downloading an eBook