9.5 Iteration D5: Degrading If Javascript Is Disabled 9.3 Iteration D3: Highlighting Changes
edit app/views/store/add_to_cart.js.rjs
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"
edit app/models/cart.rb
class Cart
attr_reader :items # <wtf linkend="wtf.attr.accessor">attr_reader</wtf>
def initialize
@items = []
end
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
current_item = CartItem.new(product)
@items << current_item
end
current_item
end
def total_price
@items.sum { |item| item.price }
end
def total_items
@items.sum { |item| item.quantity }
end
end
ls -p app
controllers/
helpers/
mailers/
models/
views/
ls -p app/helpers
application_helper.rb
products_helper.rb
store_helper.rb
edit app/views/layouts/store.html.erb
<%= hidden_div_if(@cart.items.empty?, :id => 'cart') do %>
<%= render(:partial => "cart", :object => @cart) %>
<% end %>
edit app/helpers/store_helper.rb
module StoreHelper
def hidden_div_if(condition, attributes = {}, &block)
if condition
attributes["style"] = "display: none"
end
content_tag("div", attributes, &block)
end
end
edit app/controllers/store_controller.rb
def empty_cart
session[:cart] = nil
redirect_to_index
end
9.5 Iteration D5: Degrading If Javascript Is Disabled 9.3 Iteration D3: Highlighting Changes