We also don’t have time to build a full-fledged admin user interface right now, so we’ll take advantage of the various Atom feed readers that exist and have our app export all the orders as an Atom feed so the customer can quickly see what’s been purchased.
Looks like instructions on creating an Atom feed should follow right there, but they are missing. On the page 182 it’s said:
What We Just Did:
We provided a feed so the administrator can monitor incoming orders.
And on the page 183:
Playtime
Here’s some stuff to try on your own:
Get HTML- and JSON-formatted views working for who_bought requests. Experiment with including the order information in the JSON view by rendering @product.to_json(include: :orders). Do the same thing for XML using ActiveModel::Serializers::Xml.2
Looks like this task is related to the Atom feed that should have been built above.
The book is missing a subsection called Iteration G2: Atom. In the previous version of the book “AWDR-6,” four pages (189-192) are dedicated to Atom feeds.
Is there anyway we can get the full missing section? I want to add an atom feed so I can do the Playtime sections properly like I have with all the previous chapters…
XML seems to be increasingly de-emphasized these days, but here is the essence of what was in that section:
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