The Depot Application

The Depot Application

9.1 Iteration D1: Moving the Cart 8.4 Iteration C3: Handling Errors

8.5 Iteration C4: Finishing the Cart

edit app/views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
  <% for item in @cart.items %>
    <li><%= item.quantity %> &times; <%= item.title %></li>
  <% end %>
</ul>
 
<%= button_to 'Empty cart', :action => 'empty_cart' %>
edit app/controllers/store_controller.rb
  def empty_cart
    session[:cart] = nil
    flash[:notice] = "Your cart is currently empty"
    redirect_to :action => 'index'
  end
get /store/empty_cart
You are being redirected.
get http://localhost:3000/store/index
Your cart is currently empty

Your Pragmatic Catalog

Auto

Pragmatic Project Automation

Pragmatic Project Automation shows you how to improve the consistency and repeatability of your project's procedures using automation to reduce risk and errors.

Simply put, we're going to put this thing called a computer to work for you doing the mundane (but important) project stuff. That means you'll have more time and energy to do the really exciting---and difficult---stuff, like writing quality code.

$29.95
Utc

Pragmatic Unit Testing (C#)

Pragmatic programmers use feedback to drive their development and personal processes. The most valuable feedback you can get while coding comes from unit testing.

Without good tests in place, coding can become a frustrating game of "whack-a-mole." That's the carnival game where the player strikes at a mechanical mole; it retreats and another mole pops up on the opposite side of the field. The moles pop up and down so fast that you end up flailing your mallet helplessly as the moles continue to pop up where you least expect them.

$27.75
Svn

Pragmatic Version Control

This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. don't use any version control at all. Many others don't use it well, and end up experiencing time-consuming problems.

$28.50
edit app/controllers/store_controller.rb
  def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
    @cart.add_product(product)
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid product #{params[:id]}")
    redirect_to_index("Invalid product")
  end
 
  def empty_cart
    session[:cart] = nil
    redirect_to_index("Your cart is currently empty")
  end
 
private
 
  def redirect_to_index(msg)
    flash[:notice] = msg
    redirect_to :action => 'index'
  end
edit app/views/store/add_to_cart.html.erb
<div class="cart-title">Your Cart</div>
<table>
  <% for item in @cart.items %>
    <tr>
      <td><%= item.quantity %>&times;</td>
      <td><%=h item.title %></td>
      <td class="item-price"><%= number_to_currency(item.price) %></td>
    </tr>
  <% end %>  
  
  <tr class="total-line">
    <td colspan="2">Total</td>
    <td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
  </tr>
 
</table>
  
<%= button_to "Empty cart", :action => :empty_cart %>
edit public/stylesheets/depot.css
/* Styles for the cart in the main page */
 
.cart-title {
  font: 120% bold;
}
 
.item-price, .total-line {
  text-align: right;
}
 
.total-line .total-cell {
  font-weight: bold;
  border-top: 1px solid #595;
}
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
      @items << CartItem.new(product)
    end
  end
 
  def total_price
    @items.sum { |item| item.price }
  end
end
get /store/add_to_cart/2
Your Cart
Pragmatic Project Automation $29.95
Total $29.95
get /store/add_to_cart/2
Your Cart
Pragmatic Project Automation $59.90
Total $59.90
get /store/add_to_cart/3
Your Cart
Pragmatic Project Automation $59.90
Pragmatic Version Control $28.50
Total $88.40

9.1 Iteration D1: Moving the Cart 8.4 Iteration C3: Handling Errors