From: Neil Smith Date: Mon, 16 Feb 2009 12:40:08 +0000 (+0000) Subject: Finished chapter 9 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=f9d45398338f9e5b2dabd768fcd793c4f81934f2;p=depot.git Finished chapter 9 --- diff --git a/app/controllers/store_controller.rb b/app/controllers/store_controller.rb index 8234cdc..3b2e6b8 100644 --- a/app/controllers/store_controller.rb +++ b/app/controllers/store_controller.rb @@ -9,7 +9,8 @@ class StoreController < ApplicationController @cart = find_cart @current_item = @cart.add_product(product) respond_to do |format| - format.js + format.js if request.xhr? + format.html {redirect_to_index} end rescue ActiveRecord::RecordNotFound logger.error("Attempt to access invalid product #{params[:id]}" ) @@ -18,7 +19,7 @@ class StoreController < ApplicationController def empty_cart session[:cart] = nil - redirect_to_index('Your cart has been emptied') + redirect_to_index unless request.xhr? end private diff --git a/app/helpers/store_helper.rb b/app/helpers/store_helper.rb index 9263619..4a9c91c 100644 --- a/app/helpers/store_helper.rb +++ b/app/helpers/store_helper.rb @@ -1,2 +1,10 @@ module StoreHelper + + def hidden_div_if(condition, attributes = {}, &block) + if condition + attributes["style"] = "display: none" + end + content_tag("div", attributes, &block) + end + end diff --git a/app/models/cart.rb b/app/models/cart.rb index 88061d3..bd51c38 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -20,4 +20,8 @@ class Cart @items.sum {|item| item.price} end + def total_items + @items.sum {|item| item.quantity} + end + end diff --git a/app/views/layouts/store.html.erb b/app/views/layouts/store.html.erb index 5f2d4f1..e55726c 100644 --- a/app/views/layouts/store.html.erb +++ b/app/views/layouts/store.html.erb @@ -14,9 +14,9 @@
-
+ <% hidden_div_if(@cart.items.empty?, :id => "cart") do %> <%= render(:partial => "cart" , :object => @cart) %> -
+ <% end %> Home
Questions
News
diff --git a/app/views/store/_cart.html.erb b/app/views/store/_cart.html.erb index 01fbeb5..cc84043 100644 --- a/app/views/store/_cart.html.erb +++ b/app/views/store/_cart.html.erb @@ -1,11 +1,13 @@
Your Cart
- - <%= render(:partial => 'cart_item', :collection => cart.items) %> +
+ <%= render(:partial => 'cart_item', :collection => cart.items.sort_by {|item| item.product.title}) %> - - - - -
Total<%= number_to_currency(cart.total_price, :unit => "£") %>
+ + Total + <%= number_to_currency(cart.total_price, :unit => "£") %> + + -<%= button_to 'Empty cart', :action => :empty_cart %> +<% form_remote_tag :url => {:action => 'empty_cart'} do %> + <%= submit_tag "Empty cart" %> +<% end %> diff --git a/app/views/store/add_to_cart.js.rjs b/app/views/store/add_to_cart.js.rjs index 1b6ec4c..2ef0a83 100644 --- a/app/views/store/add_to_cart.js.rjs +++ b/app/views/store/add_to_cart.js.rjs @@ -1,5 +1,7 @@ page.replace_html("cart", :partial => 'cart', :object => @cart) +page[:cart].visual_effect :blind_down if @cart.total_items == 1 + page[:current_item].visual_effect :highlight, :startcolor => "#88ff88" , :endcolor => "#114411" diff --git a/app/views/store/empty_cart.js.rjs b/app/views/store/empty_cart.js.rjs new file mode 100644 index 0000000..5f9616f --- /dev/null +++ b/app/views/store/empty_cart.js.rjs @@ -0,0 +1,2 @@ + +page[:cart].visual_effect :blind_up diff --git a/app/views/store/index.html.erb b/app/views/store/index.html.erb index fff01df..f3e7ebe 100644 --- a/app/views/store/index.html.erb +++ b/app/views/store/index.html.erb @@ -2,8 +2,10 @@ <% for product in @products -%>
- <%= link_to image_tag(product.image_url), {:action => "add_to_cart", :id => product}, :method => "post" %> -

<%= link_to h(product.title), {:action => "add_to_cart", :id => product}, :method => "post" %>

+ <% form_remote_tag :url => {:action => 'add_to_cart', :id => product} do %> + <%= image_submit_tag(product.image_url) %> + <% end %> +

<%= h(product.title) %>

<%= product.description %>
<%= number_to_currency product.price, :unit => "£" %> diff --git a/public/stylesheets/depot.css b/public/stylesheets/depot.css index a8d7cc6..bb4934d 100644 --- a/public/stylesheets/depot.css +++ b/public/stylesheets/depot.css @@ -125,7 +125,7 @@ h1 { font-family: sans-serif; } -#store .entry img { +#store .entry img, #store .entry form input[type="image"] { width: 75px; float: left; border: 0;