From: Neil Smith <neil.git@njae.me.uk>
Date: Mon, 9 Mar 2009 11:18:12 +0000 (+0000)
Subject: Decoupled carts and orders
X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=05c481a0756c1fa0a9956b21a912de2c10914ef2;p=depot.git

Decoupled carts and orders
---

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] -%>
             <div id="notice"><%= flash[:notice] %></div>
         <% end -%>
-        
-        <%= yield :layout %>
+        <div id="main_panel">
+          <%= yield :layout %>
+        </div>
       </div>
     </div>
   </body>
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 @@
   </tr>
 </table>
 
-<% form_remote_tag :url => {:action => 'checkout'} do %>
-    <%= submit_tag "Checkout" %>
-<% end %>
+<% unless @during_checkout %>
+  <div id="checkout_buttons">
+    <% 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 %>
+  </div>
 <% 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 @@
-<div id="main_panel">
 <h1>Neil's Whimsical Store Catalogue</h1>
 
 <% for product in @products -%>
@@ -16,4 +15,4 @@
     </div>
   </div>
 <% end %>
-</div>
+