From 893fb158ba822c7ce7f5b3d011a2401e7f974f50 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 18 Mar 2009 12:39:58 +0000 Subject: [PATCH] Finished Chapter 12 --- app/controllers/info_controller.rb | 17 ++++++++++++ app/helpers/info_helper.rb | 2 ++ app/models/product.rb | 3 +++ app/views/info/who_bought.atom.builder | 36 +++++++++++++++++++++++++ app/views/info/who_bought.html.erb | 9 +++++++ app/views/info/who_bought.xml.builder | 8 ++++++ app/views/orders/index.html.erb | 2 +- test/functional/info_controller_test.rb | 8 ++++++ 8 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 app/controllers/info_controller.rb create mode 100644 app/helpers/info_helper.rb create mode 100644 app/views/info/who_bought.atom.builder create mode 100644 app/views/info/who_bought.html.erb create mode 100644 app/views/info/who_bought.xml.builder create mode 100644 test/functional/info_controller_test.rb 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 @@ +

People Who Bought <%= @product.title %>

+ + \ 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 @@
Order <%=h order.id %>
<%=h order.name %>
<%=h order.email %>
-
<%=h order.address %>
+
<%=(h order.address).gsub(/\n/, "
") %>
<%=h order.pay_type %>
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 -- 2.34.1