Agile Web Development with Rails, Edition 5

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

12.4 Playtime

add activemodel-serializers-xml

edit Gemfile
gem 'activemodel-serializers-xml'
bundle install --local
Resolving dependencies...
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.1
Using minitest 5.3.3
Using thread_safe 0.3.6
Using builder 3.2.3
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using rack 2.0.1
Using nio4r 2.0.0
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 7.1.4
Using bundler 1.14.6
Using byebug 9.0.6
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using method_source 0.8.2
Using thor 0.19.4
Using debug_inspector 0.0.2
Using ffi 1.9.18
Using multi_json 1.12.1
Using pg 0.19.0
Using puma 3.8.2
Using sass 3.4.23
Using tilt 2.0.7
Using sqlite3 1.3.13
Using turbolinks-source 5.0.0
Using tzinfo 1.2.3
Using nokogiri 1.7.1
Using rack-test 0.6.3
Using sprockets 3.7.1
Using websocket-driver 0.6.5
Using mime-types 3.1
Using coffee-script 2.4.1
Using uglifier 3.1.10
Using rb-inotify 0.9.7 from source at `/home/rubys/git/rb-inotify`
Using queue_classic 3.2.0.RC1 from source at `/home/rubys/git/queue_classic`
Using turbolinks 5.0.1
Using activesupport 5.0.2 from source at `/home/rubys/git/rails`
Using loofah 2.0.3
Using mail 2.6.4
Using rails-dom-testing 2.0.2
Using globalid 0.3.7
Using activemodel 5.0.2 from source at `/home/rubys/git/rails`
Using jbuilder 2.6.3
Using spring 2.0.1
Using rails-html-sanitizer 1.0.3
Using activejob 5.0.2 from source at `/home/rubys/git/rails`
Using activerecord 5.0.2 from source at `/home/rubys/git/rails`
Using actionview 5.0.2 from source at `/home/rubys/git/rails`
Using activemodel-serializers-xml 1.0.1
Using actionpack 5.0.2 from source at `/home/rubys/git/rails`
Using actioncable 5.0.2 from source at `/home/rubys/git/rails`
Using actionmailer 5.0.2 from source at `/home/rubys/git/rails`
Using railties 5.0.2 from source at `/home/rubys/git/rails`
Using sprockets-rails 3.2.0
Using coffee-rails 4.2.1
Using jquery-rails 4.3.1
Using jquery-ui-rails 6.0.1
Using web-console 3.4.0 from source at `/home/rubys/git/web-console`
Using rails 5.0.2 from source at `/home/rubys/git/rails`
Using sass-rails 5.0.6
Bundle complete! 17 Gemfile dependencies, 63 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Restart the server.

include xml serializers

edit app/models/product.rb
require 'active_model/serializers/xml'
class Product < ApplicationRecord
  include ActiveModel::Serializers::Xml
end
edit app/models/order.rb
require 'active_model/serializers/xml'
class Order < ApplicationRecord
  include ActiveModel::Serializers::Xml
end

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-03-26T19:42:29Z</updated>
  <entry>
    <id>tag:localhost,2005:Order/1</id>
    <published>2017-03-26T19:42:29Z</published>
    <updated>2017-03-26T19:42:29Z</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-03-26T19:42:29Z</published>
    <updated>2017-03-26T19:42:29Z</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-03-26T19:40:46Z</created-at>
  <updated-at type="dateTime">2017-03-26T19:40:46Z</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">2017-03-26T19:42:29Z</created-at>
      <updated-at type="dateTime">2017-03-26T19:42:29Z</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">2017-03-26T19:42:29Z</created-at>
      <updated-at type="dateTime">2017-03-26T19:42:29Z</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 name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="aLKL3gIIOKhfTahGc7Y2Rj0IxpUsKfsRAAvpy8xxJdjEWHwTmVaIH8NH6E0+rSXdzo+cGX8yrdG03DdlT1wENQ==" />
 
    <meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="XeF2QARt6xmScoZ+1MOrtfn3T8X3AhTUgW17CWrIUvLxC4GNnzNbrg54xnWZ2LguCnAVSaQZQhQ1uqWn6eVzHw==" />
 
    <link rel="stylesheet" media="all" href="/assets/carts.self-636a1ce94212257d22a76a210776c04b91a129482206d89f9e61e1ca384c9775.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/line_items.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/orders.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/products.self-72a20feef6d26390ded2d0e2e5bf1237a2c21049aca1403a9868b8d23540f2eb.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/scaffolds.self-f34c2a4988e752f1f5e3fe7a71ab3ad5c73bb348085fc81a9352f7fd5ddee32c.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/store.self-b89ffbc9d886d991e0f3cb4cd39a5506ef5f64e40e75c2dd25559ca1ddc2f0a8.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/application.self-69c245a005d2c7c9119e9d23429533ab5275835b3996117d89e8321bf868197a.css?body=1" data-turbolinks-track="reload" />
    <script src="/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/version.self-c8e3d1203da26ea7efdf83c1eabb3f0ba55cb68e463f5ccf0d77bd15ce6a8e61.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/effect.self-776455da1682afff3a4974146aa96ca840597b879ed3797af0604063527443c6.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery-ui/effects/effect-blind.self-2f8923f5c3073717dad35ac1f9bd4dbc13d2f9c8128f7dbea9819921338271f4.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/turbolinks.self-c5acd7a204f5f25ce7a1d8a0e4d92e28d34c9e2df2c7371cd7af88e147e4ad82.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable.self-50cf57bdf719a6c60243f570fdc69cc3b8971d9b5d91d1f3e8fb0edde60c63a3.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable/connection_monitor.self-dc0643774d220a91b7ba63973ddbc09f4a52965221d646c7830a22fb96ab7e58.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable/connection.self-0847329926e139a5dbf24e5bb3c08e6206e5c071b877d6cad7da20e55501ed2d.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable/subscriptions.self-5c7dd80f075eacd4fc06ad385498d4c3a354011b505d8a8cef90df105c361c80.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable/subscription.self-0a82319db058bc79ff11535c02d44534daf61a646a32010e44e671e7181cc742.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable/consumer.self-def499e29967995b51c5ad790077021a5b1980f2e3330f4ac33010f6813867cd.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/cable.self-6e0514260c1aa76eaf252412ce74e63f68819fd19bf740595f592c5ba4c36537.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/channels/products.self-a36983cdb45f5c31825f7425c6d56e16b232ff19a2fdac5da7fa48917f824ea7.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/carts.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/line_items.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/orders.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/products.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/store.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/application.self-b5f8abbb026842e6f126160e1d1b70143792680186f4176fa4ea063de99e552c.js?body=1" data-turbolinks-track="reload"></script>
  </head>
 
  <body class="products">
    <div id="banner">
      <img alt="The Pragmatic Bookshelf" src="/assets/logo-a8906fb6c933f00883d2ad0385ae52201c0e1d1f9235c37f4a5762f02ad80936.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-03-26T19:40:46.305Z","updated_at":"2017-03-26T19:40:46.305Z","orders":[{"id":1,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-03-26T19:42:29.129Z","updated_at":"2017-03-26T19:42:29.129Z"},{"id":2,"name":"Dave Thomas","address":"123 Main St","email":"customer@example.com","pay_type":"Check","created_at":"2017-03-26T19:42:29.708Z","updated_at":"2017-03-26T19:42:29.708Z"}]}

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

rails test
Run options: --seed 61925
 
# Running:
 
.......................................
 
Finished in 0.849172s, 45.9271 runs/s, 91.8542 assertions/s.
 
39 runs, 78 assertions, 0 failures, 0 errors, 0 skips

Commit

git commit -a -m "Orders"
[master fe025f3] Orders
 7 files changed, 83 insertions(+), 43 deletions(-)
git tag iteration-g

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