--- /dev/null
+class InfoController < ApplicationController
+ def who_bought
+ @product = Product.find(params[:id])
+ @orders = @product.orders
+ respond_to do |format|
+ format.html
+ format.xml {render :layout => false}
+ format.atom {render :layout => false}
+ format.json {render :layout => false, :json => @product.to_json(:include => :orders) }
+ end
+ end
+
+protected
+
+ def authorize
+ end
+end
--- /dev/null
+module InfoHelper
+end
class Product < ActiveRecord::Base
+ has_many :line_items
+ has_many :orders, :through => :line_items
+
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validate :price_must_be_at_least_a_penny
--- /dev/null
+atom_feed do |feed|
+ feed.title "Who bought #{@product.title}"
+ feed.updated @orders.first.created_at
+ for order in @orders
+ 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
+ for item in order.line_items
+ 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|
+ entry.name order.name
+ entry.email order.email
+ end
+ end
+ end
+end
\ No newline at end of file
--- /dev/null
+<h3>People Who Bought <%= @product.title %></h3>
+
+<ul>
+ <% for order in @orders -%>
+ <li>
+ <%= mail_to order.email, order.name %>
+ </li>
+ <% end -%>
+</ul>
\ No newline at end of file
--- /dev/null
+xml.order_list(:for_product => @product.title) do
+ for o in @orders
+ xml.order do
+ xml.name(o.name)
+ xml.email(o.email)
+ end
+ end
+end
\ No newline at end of file
<dt>Order <%=h order.id %></dt>
<dd><%=h order.name %></dd>
<dd><%=h order.email %></dd>
- <dd><%=h order.address %></dd>
+ <dd><%=(h order.address).gsub(/\n/, "<br />") %></dd>
<dd><%=h order.pay_type %></dd>
</dl>
</td>
--- /dev/null
+require 'test_helper'
+
+class InfoControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end