From aff195f08c5fc2b8db74058b34720164148cfb4e Mon Sep 17 00:00:00 2001 From: Neil Smith <neil.git@njae.me.uk> Date: Sat, 14 Feb 2009 15:14:44 +0000 Subject: [PATCH] Finished iteration D.3 in chapter 9 --- app/controllers/store_controller.rb | 10 +++++++--- app/models/cart.rb | 4 +++- app/views/layouts/store.html.erb | 4 ++++ app/views/store/_cart.html.erb | 11 +++++++++++ app/views/store/_cart_item.html.erb | 9 +++++++++ app/views/store/add_to_cart.html.erb | 18 ------------------ app/views/store/add_to_cart.js.rjs | 5 +++++ app/views/store/index.html.erb | 4 +++- public/stylesheets/depot.css | 16 ++++++++++++++++ 9 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 app/views/store/_cart.html.erb create mode 100644 app/views/store/_cart_item.html.erb delete mode 100644 app/views/store/add_to_cart.html.erb create mode 100644 app/views/store/add_to_cart.js.rjs diff --git a/app/controllers/store_controller.rb b/app/controllers/store_controller.rb index f010326..8234cdc 100644 --- a/app/controllers/store_controller.rb +++ b/app/controllers/store_controller.rb @@ -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 diff --git a/app/models/cart.rb b/app/models/cart.rb index 0f1d52e..88061d3 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -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 diff --git a/app/views/layouts/store.html.erb b/app/views/layouts/store.html.erb index 77e86d9..5f2d4f1 100644 --- a/app/views/layouts/store.html.erb +++ b/app/views/layouts/store.html.erb @@ -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 index 0000000..01fbeb5 --- /dev/null +++ b/app/views/store/_cart.html.erb @@ -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 index 0000000..dc87e18 --- /dev/null +++ b/app/views/store/_cart_item.html.erb @@ -0,0 +1,9 @@ +<% if cart_item == @current_item %> + <tr id="current_item"> +<% else %> + <tr> +<% end %> + <td><%= cart_item.quantity %>×</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 index 64270c3..0000000 --- a/app/views/store/add_to_cart.html.erb +++ /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 %>×</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 index 0000000..1b6ec4c --- /dev/null +++ b/app/views/store/add_to_cart.js.rjs @@ -0,0 +1,5 @@ +page.replace_html("cart", :partial => 'cart', :object => @cart) + +page[:current_item].visual_effect :highlight, + :startcolor => "#88ff88" , + :endcolor => "#114411" diff --git a/app/views/store/index.html.erb b/app/views/store/index.html.erb index c65b69b..fff01df 100644 --- a/app/views/store/index.html.erb +++ b/app/views/store/index.html.erb @@ -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 %> diff --git a/public/stylesheets/depot.css b/public/stylesheets/depot.css index bf77b4b..a8d7cc6 100644 --- a/public/stylesheets/depot.css +++ b/public/stylesheets/depot.css @@ -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 */ -- 2.43.0