Finished iteration D.3 in chapter 9
authorNeil Smith <neil.git@njae.me.uk>
Sat, 14 Feb 2009 15:14:44 +0000 (15:14 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 14 Feb 2009 15:14:44 +0000 (15:14 +0000)
app/controllers/store_controller.rb
app/models/cart.rb
app/views/layouts/store.html.erb
app/views/store/_cart.html.erb [new file with mode: 0644]
app/views/store/_cart_item.html.erb [new file with mode: 0644]
app/views/store/add_to_cart.html.erb [deleted file]
app/views/store/add_to_cart.js.rjs [new file with mode: 0644]
app/views/store/index.html.erb
public/stylesheets/depot.css

index f0103268556578461452c8c2bfa7b834c71600eb..8234cdcb244d053025bcc790324fc63b1b74352e 100644 (file)
@@ -1,12 +1,16 @@
 class StoreController < ApplicationController
   def index
     @products = Product.find_products_for_sale
+    @cart = find_cart
   end
 
   def add_to_cart
     product = Product.find(params[:id])
     @cart = find_cart
-    @cart.add_product(product)
+    @current_item = @cart.add_product(product)
+      respond_to do |format|
+        format.js
+      end
   rescue ActiveRecord::RecordNotFound
     logger.error("Attempt to access invalid product #{params[:id]}" )
     redirect_to_index('Invalid product')
@@ -23,8 +27,8 @@ class StoreController < ApplicationController
     session[:cart] ||= Cart.new
   end
 
-  def redirect_to_index(msg)
-    flash[:notice] = msg
+  def redirect_to_index(msg = nil)
+    flash[:notice] = msg if msg
     redirect_to :action => 'index'
   end
   
index 0f1d52e731588dd79ac0bde3fe487761d93ad3be..88061d380470188dcf6991dcd5e0cdb3f966799d 100644 (file)
@@ -10,8 +10,10 @@ class Cart
     if current_item
       current_item.increment_quantity
     else
-      @items << CartItem.new(product)
+      current_item = CartItem.new(product)
+      @items << current_item
     end
+    current_item
   end
   
   def total_price
index 77e86d9e23e08684c1cbb69f3c329f337ed50fd0..5f2d4f10bd04a50ff6cedb36881cd420bc9fd805 100644 (file)
@@ -5,6 +5,7 @@
   <head>
     <title>Neil's Whimsical Online Store</title>
     <%= stylesheet_link_tag "depot" , :media => "all" %>
+    <%= javascript_include_tag :defaults %>
   </head>
   <body id="store">
     <div id="banner">
@@ -13,6 +14,9 @@
     </div>
     <div id="columns">
       <div id="side">
+        <div id="cart">
+            <%= render(:partial => "cart" , :object => @cart) %>
+        </div>
         <a href="http://www....">Home</a><br />
         <a href="http://www..../faq">Questions</a><br />
         <a href="http://www..../news">News</a><br />
diff --git a/app/views/store/_cart.html.erb b/app/views/store/_cart.html.erb
new file mode 100644 (file)
index 0000000..01fbeb5
--- /dev/null
@@ -0,0 +1,11 @@
+<div class="cart-title">Your Cart</div>
+    <table>
+    <%= render(:partial => 'cart_item', :collection => cart.items) %>
+
+        <tr class="total-line">
+            <td colspan="2">Total</td>
+            <td class="total-cell"><%= number_to_currency(cart.total_price, :unit => "£") %></td>
+        </tr>
+    </table>
+
+<%= button_to 'Empty cart', :action => :empty_cart %>
diff --git a/app/views/store/_cart_item.html.erb b/app/views/store/_cart_item.html.erb
new file mode 100644 (file)
index 0000000..dc87e18
--- /dev/null
@@ -0,0 +1,9 @@
+<% if cart_item == @current_item %>
+  <tr id="current_item">
+<% else %>
+  <tr>
+<% end %>
+  <td><%= cart_item.quantity %>&times;</td>
+  <td><%=h cart_item.title %></td>
+  <td class="item-price"><%= number_to_currency(cart_item.price, :unit => "£") %></td>
+</tr>
diff --git a/app/views/store/add_to_cart.html.erb b/app/views/store/add_to_cart.html.erb
deleted file mode 100644 (file)
index 64270c3..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-<h2>Your Whimsical Cart</h2>
-
-<div class="cart-title">Your Cart</div>
-    <table>
-    <% for item in @cart.items %>
-        <tr>
-            <td><%= item.quantity %>&times;</td>
-            <td><%=h item.title %></td>
-            <td class="item-price"><%= number_to_currency(item.price, :unit => "£") %></td>
-        </tr>
-    <% end %>
-        <tr class="total-line">
-            <td colspan="2">Total</td>
-            <td class="total-cell"><%= number_to_currency(@cart.total_price, :unit => "£") %></td>
-        </tr>
-    </table>
-
-<%= button_to 'Empty cart', :action => :empty_cart %>
diff --git a/app/views/store/add_to_cart.js.rjs b/app/views/store/add_to_cart.js.rjs
new file mode 100644 (file)
index 0000000..1b6ec4c
--- /dev/null
@@ -0,0 +1,5 @@
+page.replace_html("cart", :partial => 'cart', :object => @cart)
+
+page[:current_item].visual_effect :highlight,
+                                  :startcolor => "#88ff88" ,
+                                  :endcolor => "#114411"
index c65b69b114087113b282c84c086517e72c043c61..fff01dffcb953c92a0c8ebd97a8042ef28c58ebb 100644 (file)
@@ -7,7 +7,9 @@
     <%= product.description %>
     <div class="price-line">
       <span class="price"><%= number_to_currency product.price, :unit => "£" %></span>
-      <%= button_to "Add to cart", :action => "add_to_cart", :id => product %>
+      <% form_remote_tag :url => {:action => 'add_to_cart', :id => product} do %>
+        <%= submit_tag "Add to cart" %>
+      <% end %>
     </div>
   </div>
 <% end %>
index bf77b4bd521c747695f222b5aea7359032a8684d..a8d7cc65b3e84cbc5a0686337609cb48c6896443 100644 (file)
@@ -184,6 +184,22 @@ h1 {
 }
 /* END:cartmain */
 
+/* START:cartside */
+/* Styles for the cart in the sidebar */
+
+#cart, #cart table {
+  font-size: smaller;
+  color:     white;
+}
+
+#cart table {
+  border-top:    1px dotted #595;
+  border-bottom: 1px dotted #595;
+  margin-bottom: 10px;
+}
+/* END:cartside */
+
+
 
 /* The error box */