Iteration E1: Creating a Smarter Cart

Một phần của tài liệu 1934356549 {88177f43} agile web development with rails (4th ed ) ruby, thomas hansson 2011 03 31 (Trang 126 - 131)

Associating a count with each product in our cart is going to require us to modify theline_itemstable. We’ve used migration before in Section6.1,Applying the Migration, on page 79to update the schema of the database. While that was as part of creating the initial scaffolding for a model, the basic approach is the same.

depot> rails generate migration add_quantity_to_line_items quantity:integer

Rails can tell from the name of the migration that you are adding one or more columns to theline_items table and can pick up the names and data types for each column from the last argument. The two patterns that Rails matches on is add_XXX_to_TABLE and remove_XXX_from_TABLE where the value of XXX is ignored; what matters is the list of column names and types that appear after the migration name.

The only thing Rails can’t tell is what a reasonable default is for this column.

In many cases, anullvalue would do, but let’s make it the value 1 for existing carts by modifying the migration before we apply it:

Download depot_g/db/migrate/20110211000004_add_quantity_to_line_items.rb

class AddQuantityToLineItems < ActiveRecord::Migration def self.up

add_column :line_items, :quantity, :integer, :default => 1 end

ITERATIONE1: CREATING ASMAR TERCAR T 127

def self.down

remove_column :line_items, :quantity end

end

Once complete, we run the migration:

depot> rake db:migrate

Now we need a smartadd_productmethod in ourCart, one that checks whether our list of items already includes the product we’re adding; if it does, it bumps the quantity, and if it doesn’t, it builds a newLineItem:

Download depot_g/app/models/cart.rb

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) end

current_item end

This code uses a clever little Active Record trick. You see that the first line of the method callsfind_by_product_id. But we don’t define a method with that name. However, Active Record notices the call to an undefined method and spots that it starts with the stringfind_byand ends with the name of a column.

It then dynamically constructs a finder method for us, adding it to our class.

We talk more about these dynamic finders starting on page283.

We also need to modify the line item controller to make use of this method:

Download depot_g/app/controllers/line_items_controller.rb

def create

@cart = current_cart

product = Product.find(params[:product_id])

@line_item = @cart.add_product(product.id) respond_to do |format|

if @line_item.save

format.html { redirect_to(@line_item.cart,

:notice => 'Line item was successfully created.') } format.xml { render :xml => @line_item,

:status => :created, :location => @line_item } else

format.html { render :action => "new" }

format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }

end end end

Report erratum this copy is(P1.0 printing, March 2011)

Download from Wow! eBook <www.wowebook.com>

ITERATIONE1: CREATING ASMAR TERCAR T 128

There’s one last quick change to theshowview to use this new information:

Download depot_g/app/views/carts/show.html.erb

<h2>Your Pragmatic Cart</h2>

<ul>

<% @cart.line_items.each do |item| %>

<li><%= item.quantity %> &times; <%= item.product.title %></li>

<% end %>

</ul>

Now that all the pieces are in place, we can go back to the store page and hit the Add to Cart button for a product that is already in the cart. What we are likely to see is a mixture of individual products listed separately and a single product listed with a quantity of two. This is because we added a quantity of 1 to existing columns instead of collapsing multiple rows when possible. What we need to do next is migrate the data.

We start by creating a migration:

depot> rails generate migration combine_items_in_cart

This time, Rails can’t infer what we are trying to do, so it is entirely up to us to fill in theself.upmethod:

Download depot_g/db/migrate/20110211000005_combine_items_in_cart.rb

def self.up

# replace multiple items for a single product in a cart with a single item Cart.all.each do |cart|

# count the number of each product in the cart

sums = cart.line_items.group(:product_id).sum(:quantity) sums.each do |product_id, quantity|

if quantity > 1

# remove individual items

cart.line_items.where(:product_id=>product_id).delete_all

# replace with a single item

cart.line_items.create(:product_id=>product_id, :quantity=>quantity) end

end end end

This is easily the most extensive code we’ve seen so far. Let’s look at it in small pieces:

• We start by iterating over each cart. iterating

֒→page61

• For each cart, we get a sum of the quantity fields for each of the line items associated with this cart, grouped byproduct_id. The resulting sums will be a list of ordered pairs ofproduct_ids and quantity.

ITERATIONE1: CREATING ASMAR TERCAR T 129

Figure 10.1: A cart with quantities

• We iterate over these sums, extracting the product_id and quantity from each.

• In cases where the quantity is greater than 1, we will delete all of the indi- vidual line items associated with this cart and this product and replace them with a single line item with the correct quantity.

Note how easily and elegantly Rails enables you to express this algorithm.

With this code in place, we apply this migration just like any other migration:

depot> rake db:migrate

We can immediately see the results by looking at the cart, as shown in Fig- ure 10.1. Although we have reason to be pleased with ourselves, we are not done yet. An important principle of migrations is that each step needs to be reversible, so we implement aself.downtoo. This method finds line items with a quantity of greater than 1; adds new line items for this cart and product, each with a quantity of 1; and finally deletes the line item. The following code accomplishes that:

Download depot_g/db/migrate/20110211000005_combine_items_in_cart.rb

def self.down

# split items with quantity>1 into multiple items LineItem.where("quantity>1").each do |line_item|

# add individual items line_item.quantity.times do

LineItem.create :cart_id=>line_item.cart_id, :product_id=>line_item.product_id, :quantity=>1 end

Report erratum this copy is(P1.0 printing, March 2011)

Download from Wow! eBook <www.wowebook.com>

ITERATIONE1: CREATING ASMAR TERCAR T 130

Figure 10.2: A cart after the migration has been rolled back

# remove original item line_item.destroy end

end

At this point, we can just as easily roll back our migration with a single com- mand:

depot> rake db:rollback

Once again, we can immediately inspect the results by looking at the cart, as shown in Figure10.2. Once we reapply the migration (with therake db:migrate command), we have a cart that maintains a count for each of the products it holds, and we have a view that displays that count.

Happy that we have something presentable, we call our customer over and show her the result of our morning’s work. She’s pleased—she can see the site starting to come together. However, she’s also troubled, having just read an article in the trade press on the way ecommerce sites are being attacked and compromised daily. She read that one kind of attack involves feeding requests with bad parameters into web applications, hoping to expose bugs and security flaws. She noticed that the link to the cart looks likecarts/nnn, where nnn is our internal cart id. Feeling malicious, she manually types this request into a browser, giving it a cart id ofwibble. She’s not impressed when our application displays the page in Figure10.3, on the following page. This reveals way too much information about our application. It also seems fairly unprofessional.

ITERATIONE2: HANDLINGERRORS 131

Figure 10.3: Our application spills its guts.

Một phần của tài liệu 1934356549 {88177f43} agile web development with rails (4th ed ) ruby, thomas hansson 2011 03 31 (Trang 126 - 131)

Tải bản đầy đủ (PDF)

(457 trang)