Agile Web Development with Rails, Edition 4

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

12.2 Iteration G2: Atom Feeds

Missing <summary type="xhtml">.
<0> expected to be
>=
<1>.

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

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 < ActiveRecord::Base
  has_many :line_items
  has_many :orders, through: :line_items
  #...
end

Add to the routes

edit config/routes.rb
Rails.application.routes.draw do
  resources :orders
  resources :line_items
  resources :carts
  get 'store/index'
 
  resources :products do
    get :who_bought, on: :member
  end
 
  # The priority is based upon order of creation:
  # first created -> highest priority.
  # See how all your routes lay out with "rake routes".
 
  # You can have the root of your site routed with "root"
  root 'store#index', as: 'store'
  # ...
end

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 CoffeeScript</title>
  <updated>2015-03-30T10:45:55Z</updated>
</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
Content-Type: application/atom+xml; charset=utf-8
Etag: W/"2546f102cf1a26e3d203e890ec5cdd06"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 44564ec5-9a8b-45fe-92bc-8533ebad8c1a
X-Runtime: 0.005402
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-03-30)
Date: Mon, 30 Mar 2015 10:45:55 GMT
Content-Length: 420
Connection: Keep-Alive
 
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/"2546f102cf1a26e3d203e890ec5cdd06"'
HTTP/1.1 304 Not Modified 
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Etag: W/"2546f102cf1a26e3d203e890ec5cdd06"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 81e10997-877e-4f53-9b8e-489baa501465
X-Runtime: 0.005150
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-03-30)
Date: Mon, 30 Mar 2015 10:45:55 GMT
Connection: Keep-Alive
 
curl --max-time 15 --silent --dump - --output /dev/null --user dave:secret http://localhost:3000/products/2/who_bought.atom -H 'If-Modified-Since: '
HTTP/1.1 200 OK 
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/atom+xml; charset=utf-8
Etag: W/"2546f102cf1a26e3d203e890ec5cdd06"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: ab674664-eda8-4c3f-a9f9-0cce6a8924a8
X-Runtime: 0.006826
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-03-30)
Date: Mon, 30 Mar 2015 10:45:55 GMT
Content-Length: 420
Connection: Keep-Alive
 

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