Finished Chapter 12
authorNeil Smith <neil.git@njae.me.uk>
Wed, 18 Mar 2009 12:39:58 +0000 (12:39 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 18 Mar 2009 12:39:58 +0000 (12:39 +0000)
app/controllers/info_controller.rb [new file with mode: 0644]
app/helpers/info_helper.rb [new file with mode: 0644]
app/models/product.rb
app/views/info/who_bought.atom.builder [new file with mode: 0644]
app/views/info/who_bought.html.erb [new file with mode: 0644]
app/views/info/who_bought.xml.builder [new file with mode: 0644]
app/views/orders/index.html.erb
test/functional/info_controller_test.rb [new file with mode: 0644]

diff --git a/app/controllers/info_controller.rb b/app/controllers/info_controller.rb
new file mode 100644 (file)
index 0000000..85d870e
--- /dev/null
@@ -0,0 +1,17 @@
+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
diff --git a/app/helpers/info_helper.rb b/app/helpers/info_helper.rb
new file mode 100644 (file)
index 0000000..a26c850
--- /dev/null
@@ -0,0 +1,2 @@
+module InfoHelper
+end
index 27fbdabe21bba36592467e18883ed7556e0f039f..4854ee5c213d4f31da3bae6e6a31466582157667 100644 (file)
@@ -1,4 +1,7 @@
 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
diff --git a/app/views/info/who_bought.atom.builder b/app/views/info/who_bought.atom.builder
new file mode 100644 (file)
index 0000000..2bc30bf
--- /dev/null
@@ -0,0 +1,36 @@
+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
diff --git a/app/views/info/who_bought.html.erb b/app/views/info/who_bought.html.erb
new file mode 100644 (file)
index 0000000..e38b4a2
--- /dev/null
@@ -0,0 +1,9 @@
+<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
diff --git a/app/views/info/who_bought.xml.builder b/app/views/info/who_bought.xml.builder
new file mode 100644 (file)
index 0000000..0703dec
--- /dev/null
@@ -0,0 +1,8 @@
+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
index bb436043211a9f87bdcd6fb43feea9ada573cde1..5e6f621ea0104c9426aef005a34cf58355748d16 100644 (file)
@@ -9,7 +9,7 @@
         <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>
diff --git a/test/functional/info_controller_test.rb b/test/functional/info_controller_test.rb
new file mode 100644 (file)
index 0000000..6a4edb2
--- /dev/null
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class InfoControllerTest < ActionController::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end