From: Neil Smith <neil.git@njae.me.uk>
Date: Wed, 18 Mar 2009 12:39:58 +0000 (+0000)
Subject: Finished Chapter 12
X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=893fb158ba822c7ce7f5b3d011a2401e7f974f50;p=depot.git

Finished Chapter 12
---

diff --git a/app/controllers/info_controller.rb b/app/controllers/info_controller.rb
new file mode 100644
index 0000000..85d870e
--- /dev/null
+++ b/app/controllers/info_controller.rb
@@ -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
index 0000000..a26c850
--- /dev/null
+++ b/app/helpers/info_helper.rb
@@ -0,0 +1,2 @@
+module InfoHelper
+end
diff --git a/app/models/product.rb b/app/models/product.rb
index 27fbdab..4854ee5 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -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
index 0000000..2bc30bf
--- /dev/null
+++ b/app/views/info/who_bought.atom.builder
@@ -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
index 0000000..e38b4a2
--- /dev/null
+++ b/app/views/info/who_bought.html.erb
@@ -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
index 0000000..0703dec
--- /dev/null
+++ b/app/views/info/who_bought.xml.builder
@@ -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
diff --git a/app/views/orders/index.html.erb b/app/views/orders/index.html.erb
index bb43604..5e6f621 100644
--- a/app/views/orders/index.html.erb
+++ b/app/views/orders/index.html.erb
@@ -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
index 0000000..6a4edb2
--- /dev/null
+++ b/test/functional/info_controller_test.rb
@@ -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