Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

11.6 Iteration F6: Testing AJAX changes 11.3 Iteration F3: Highlighting Changes

11.4 Iteration F4: Hide an Empty Cart

Add a blind down visual effect on the first item

edit app/views/line_items/create.js.rjs
page.replace_html('cart', render(@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 < ActiveRecord::Base
  has_many :line_items, :dependent => :destroy
 
  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(:product_id => product_id)
      current_item.price = current_item.product.price
    end
    current_item
  end
 
  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end
 
    def total_items
      line_items.sum(:quantity)
    end
end

Look at the app/helpers director

ls -p app
controllers/
helpers/
mailers/
models/
views/
ls -p app/helpers
application_helper.rb
carts_helper.rb
line_items_helper.rb
products_helper.rb
store_helper.rb

Call a hidden_div_if helper

edit app/views/layouts/application.html.erb
      <%= hidden_div_if(@cart.line_items.empty?, :id => 'cart') do %>
        <%= render @cart %>
      <% end %>

Implement helper

edit app/helpers/application_helper.rb
module ApplicationHelper
  def hidden_div_if(condition, attributes = {}, &block)
    if condition
      attributes["style"] = "display: none"
    end
    content_tag("div", attributes, &block)
  end
end

Remove notice

edit app/controllers/carts_controller.rb
  def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil
 
    respond_to do |format|
      format.html { redirect_to(store_url) }
      format.xml  { head :ok }
    end
  end

Demonstrate emptying and recreating the cart

get /carts/2
Your Cart
CoffeeScript $72.00
Programming Ruby 1.9 $99.90
Total $171.90
Your Cart
CoffeeScript $72.00
Programming Ruby 1.9 $99.90
Total $171.90
post /carts/2
You are being redirected.
get http://localhost:3000/

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95
get /

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95
post /line_items?product_id=2
You are being redirected.
get http://localhost:3000/
Your Cart
CoffeeScript $36.00
Total $36.00

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

11.6 Iteration F6: Testing AJAX changes 11.3 Iteration F3: Highlighting Changes