From 05c481a0756c1fa0a9956b21a912de2c10914ef2 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 9 Mar 2009 11:18:12 +0000 Subject: [PATCH] Decoupled carts and orders --- app/controllers/store_controller.rb | 11 ++++++++++- app/models/line_item.rb | 14 +++++++------- app/models/order.rb | 12 ++++++------ app/views/layouts/store.html.erb | 5 +++-- app/views/store/_cart.html.erb | 14 +++++++++----- app/views/store/checkout.js.rjs | 4 ++++ app/views/store/index.html.erb | 3 +-- 7 files changed, 40 insertions(+), 23 deletions(-) diff --git a/app/controllers/store_controller.rb b/app/controllers/store_controller.rb index de9e160..68939e6 100644 --- a/app/controllers/store_controller.rb +++ b/app/controllers/store_controller.rb @@ -28,6 +28,7 @@ class StoreController < ApplicationController redirect_to_index("Your cart is empty" ) else @order = Order.new + @during_checkout = true respond_to do |format| format.js if request.xhr? format.html @@ -38,13 +39,21 @@ class StoreController < ApplicationController def save_order @cart = find_cart @order = Order.new(params[:order]) - @order.add_line_items_from_cart(@cart) + # @order.add_line_items_from_cart(@cart) + @cart.items.each do |item| + li = LineItem.new + li.product = item.product + li.quantity = item.quantity + li.total_price = item.price + @order.line_items << li + end if @order.save session[:cart] = nil redirect_to_index("Thank you for your order") else render :action => 'checkout' end + @during_checkout = false end diff --git a/app/models/line_item.rb b/app/models/line_item.rb index 61161a0..3e3afed 100644 --- a/app/models/line_item.rb +++ b/app/models/line_item.rb @@ -2,11 +2,11 @@ class LineItem < ActiveRecord::Base belongs_to :order belongs_to :product - def self.from_cart_item(cart_item) - li = self.new - li.product = cart_item.product - li.quantity = cart_item.quantity - li.total_price = cart_item.price - li - end +# def self.from_cart_item(cart_item) +# li = self.new +# li.product = cart_item.product +# li.quantity = cart_item.quantity +# li.total_price = cart_item.price +# li +# end end diff --git a/app/models/order.rb b/app/models/order.rb index bea4da6..3da7154 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -11,10 +11,10 @@ class Order < ActiveRecord::Base validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp, value| value} - def add_line_items_from_cart(cart) - cart.items.each do |item| - li = LineItem.from_cart_item(item) - line_items << li - end - end +# def add_line_items_from_cart(cart) +# cart.items.each do |item| +# li = LineItem.from_cart_item(item) +# line_items << li +# end +# end end diff --git a/app/views/layouts/store.html.erb b/app/views/layouts/store.html.erb index 63c7dd9..b1f928e 100644 --- a/app/views/layouts/store.html.erb +++ b/app/views/layouts/store.html.erb @@ -26,8 +26,9 @@ <% if flash[:notice] -%>
<%= flash[:notice] %>
<% end -%> - - <%= yield :layout %> +
+ <%= yield :layout %> +
diff --git a/app/views/store/_cart.html.erb b/app/views/store/_cart.html.erb index 43e9a16..313d23e 100644 --- a/app/views/store/_cart.html.erb +++ b/app/views/store/_cart.html.erb @@ -8,10 +8,14 @@ -<% form_remote_tag :url => {:action => 'checkout'} do %> - <%= submit_tag "Checkout" %> -<% end %> +<% unless @during_checkout %> +
+ <% form_remote_tag :url => {:action => 'checkout'} do %> + <%= submit_tag "Checkout" %> + <% end %> -<% form_remote_tag :url => {:action => 'empty_cart'} do %> - <%= submit_tag "Empty cart" %> + <% form_remote_tag :url => {:action => 'empty_cart'} do %> + <%= submit_tag "Empty cart" %> + <% end %> +
<% end %> diff --git a/app/views/store/checkout.js.rjs b/app/views/store/checkout.js.rjs index d819e16..610f9b1 100644 --- a/app/views/store/checkout.js.rjs +++ b/app/views/store/checkout.js.rjs @@ -1 +1,5 @@ page.replace_html("main_panel", :partial => "checkout", :object => @order) + +if page[:during_checkout] + page.select("div#checkout_buttons").each {|div| div.hide} +end diff --git a/app/views/store/index.html.erb b/app/views/store/index.html.erb index 63c12da..7bbc2ab 100644 --- a/app/views/store/index.html.erb +++ b/app/views/store/index.html.erb @@ -1,4 +1,3 @@ -

Neil's Whimsical Store Catalogue

<% for product in @products -%> @@ -16,4 +15,4 @@
<% end %> - + -- 2.34.1