Agile Web Development with Rails, Edition 5

12.3 Iteration G3: Downloading an eBook 12.1 Iteration G1: Capturing an Order

12.2 Iteration G2: Atom Feeds

Demonstrate various respond_to/format options, as well as "through" relations and basic authentication.

Define a "who_bought" member action

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.atom
      end
    end
  end

Define an Atom view (using the Atom builder)

edit app/views/products/who_bought.atom.builder
atom_feed do |feed|
  feed.title "Who bought #{@product.title}"
 
  feed.updated @latest_order.try(:updated_at) 
 
  @product.orders.each do |order|
    feed.entry(order) do |entry|
      entry.title "Order #{order.id}"
      entry.summary type: 'xhtml' do |xhtml|
        xhtml.p "Shipped to #{order.address}"
 
        xhtml.table do
          xhtml.tr do
            xhtml.th 'Product'
            xhtml.th 'Quantity'
            xhtml.th 'Total Price'
          end
          order.line_items.each do |item|
            xhtml.tr do
              xhtml.td item.product.title
              xhtml.td item.quantity
              xhtml.td number_to_currency item.total_price
            end
          end
          xhtml.tr do
            xhtml.th 'total', colspan: 2
            xhtml.th number_to_currency \
              order.line_items.map(&:total_price).sum
          end
        end
 
        xhtml.p "Paid by #{order.pay_type}"
      end
      entry.author do |author|
        author.name order.name
        author.email order.email
      end
    end
  end
end

Add "orders" to the Product class

edit app/models/product.rb
class Product < ApplicationRecord
  has_many :line_items
  has_many :orders, through: :line_items
  #...
end

Add to the routes

edit config/routes.rb

Fetch the Atom feed

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>
</feed>

Look at the headers

curl --max-time 15 --silent --dump - --output /dev/null --user dave:secret http://localhost:3000/products/2/who_bought.atom
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: W/"5ec1564f88f0b480ddffdf07101ed32f"
Last-Modified: Sun, 26 Mar 2017 19:42:29 GMT
Content-Type: application/atom+xml; charset=utf-8
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: e8c7f625-0109-498c-a25e-57ca698f734e
X-Runtime: 0.011212
Transfer-Encoding: chunked
 
curl --max-time 15 --silent --dump - --output /dev/null --user dave:secret http://localhost:3000/products/2/who_bought.atom -H 'If-None-Match: W/"5ec1564f88f0b480ddffdf07101ed32f"'
HTTP/1.1 304 Not Modified
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: W/"5ec1564f88f0b480ddffdf07101ed32f"
Last-Modified: Sun, 26 Mar 2017 19:42:29 GMT
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 22786315-f151-43c6-9dd2-c0fdc9a88085
X-Runtime: 0.004489
 
curl --max-time 15 --silent --dump - --output /dev/null --user dave:secret http://localhost:3000/products/2/who_bought.atom -H 'If-Modified-Since: Sun, 26 Mar 2017 19:42:29 GMT'
HTTP/1.1 304 Not Modified
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: W/"5ec1564f88f0b480ddffdf07101ed32f"
Last-Modified: Sun, 26 Mar 2017 19:42:29 GMT
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 4af1005f-c46f-44b3-bd0e-dadc629694e1
X-Runtime: 0.005282
 

12.3 Iteration G3: Downloading an eBook 12.1 Iteration G1: Capturing an Order