@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]}" )
def empty_cart
session[:cart] = nil
- redirect_to_index('Your cart has been emptied')
+ redirect_to_index unless request.xhr?
end
private
module StoreHelper
+
+ def hidden_div_if(condition, attributes = {}, &block)
+ if condition
+ attributes["style"] = "display: none"
+ end
+ content_tag("div", attributes, &block)
+ end
+
end
@items.sum {|item| item.price}
end
+ def total_items
+ @items.sum {|item| item.quantity}
+ end
+
end
</div>
<div id="columns">
<div id="side">
- <div id="cart">
+ <% hidden_div_if(@cart.items.empty?, :id => "cart") do %>
<%= render(:partial => "cart" , :object => @cart) %>
- </div>
+ <% end %>
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
<div class="cart-title">Your Cart</div>
- <table>
- <%= render(:partial => 'cart_item', :collection => cart.items) %>
+<table>
+ <%= render(:partial => 'cart_item', :collection => cart.items.sort_by {|item| item.product.title}) %>
- <tr class="total-line">
- <td colspan="2">Total</td>
- <td class="total-cell"><%= number_to_currency(cart.total_price, :unit => "£") %></td>
- </tr>
- </table>
+ <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 %>
+<% form_remote_tag :url => {:action => 'empty_cart'} do %>
+ <%= submit_tag "Empty cart" %>
+<% end %>
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"
--- /dev/null
+
+page[:cart].visual_effect :blind_up
<% for product in @products -%>
<div class="entry">
- <%= link_to image_tag(product.image_url), {:action => "add_to_cart", :id => product}, :method => "post" %>
- <h3><%= link_to h(product.title), {:action => "add_to_cart", :id => product}, :method => "post" %></h3>
+ <% form_remote_tag :url => {:action => 'add_to_cart', :id => product} do %>
+ <%= image_submit_tag(product.image_url) %>
+ <% end %>
+ <h3><%= h(product.title) %></h3>
<%= product.description %>
<div class="price-line">
<span class="price"><%= number_to_currency product.price, :unit => "£" %></span>
font-family: sans-serif;
}
-#store .entry img {
+#store .entry img, #store .entry form input[type="image"] {
width: 75px;
float: left;
border: 0;