From aff195f08c5fc2b8db74058b34720164148cfb4e Mon Sep 17 00:00:00 2001 From: Neil Smith 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 @@ Neil's Whimsical Online Store <%= stylesheet_link_tag "depot" , :media => "all" %> + <%= javascript_include_tag :defaults %>
+
+ <%= render(:partial => "cart" , :object => @cart) %> +
Home
Questions
News
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 @@ +
Your Cart
+ + <%= render(:partial => 'cart_item', :collection => cart.items) %> + + + + + +
Total<%= number_to_currency(cart.total_price, :unit => "£") %>
+ +<%= 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 %> + +<% else %> + +<% end %> + <%= cart_item.quantity %>× + <%=h cart_item.title %> + <%= number_to_currency(cart_item.price, :unit => "£") %> + 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 @@ -

Your Whimsical Cart

- -
Your Cart
- - <% for item in @cart.items %> - - - - - - <% end %> - - - - -
<%= item.quantity %>×<%=h item.title %><%= number_to_currency(item.price, :unit => "£") %>
Total<%= number_to_currency(@cart.total_price, :unit => "£") %>
- -<%= 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 %>
<%= number_to_currency product.price, :unit => "£" %> - <%= 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 %>
<% 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.34.1