Skip to content

Commit

Permalink
More item specs
Browse files Browse the repository at this point in the history
  • Loading branch information
David Padilla committed Sep 27, 2011
1 parent cf45a97 commit b72243d
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 417 deletions.
30 changes: 28 additions & 2 deletions features/shopping_cart.feature
Expand Up @@ -4,17 +4,43 @@ Feature: Shopping Cart
Given a product "Apple" exists
And a shopping cart exists

Scenario: Add product to cart, add it again, then remove it
Scenario: Cart totals
When I add product "Apple" to cart with price "99.99"
Then the subtotal for the cart should be "99.99"
And the total for the cart should be "108.24"
And the total unique items on the cart should be "1"

Scenario: Add a product to cart twice
When I add product "Apple" to cart with price "99.99"
And I add product "Apple" to cart with price "99.99"
Then the subtotal for the cart should be "199.98"
Then the total for the cart should be "216.48"
And the total unique items on the cart should be "2"

Scenario: Remove products from cart
Given I add 3 "Apple" products to cart with price "99.99"
When I remove 1 "Apple" unit from cart
Then the total unique items on the cart should be "1"
Then the total unique items on the cart should be "2"
When I remove 99 "Apple" units from cart
Then the total unique items on the cart should be "0"
And cart should be empty

Scenario: Totals for a single item
Given I add 3 "Apple" products to cart with price "99.99"
Then the subtotal for "Apple" on the cart should be "299.97"
And the quantity for "Apple" on the cart should be "3"
And the price for "Apple" on the cart should be "99.99"

Scenario: Subtotal for a product that is not on cart
Then the subtotal for "Apple" on the cart should be "0"

Scenario: Update the quantity of a cart item
Given I add 99 "Apple" products to cart with price "99.99"
When I update the "Apple" quantity to "2"
Then the quantity for "Apple" on the cart should be "2"

Scenario: Update the price of a cart item
Given I add 99 "Apple" products to cart with price "99.99"
When I update the "Apple" price to "10.99"
Then the price for "Apple" on the cart should be "10.99"

51 changes: 43 additions & 8 deletions features/step_definitions/shopping_cart_steps.rb
Expand Up @@ -3,12 +3,12 @@
end

Then /^the total for the cart should be "([^"]*)"$/ do |total|
@cart.reload
@cart.reload
@cart.total.should eq(total.to_f)
end

Then /^the subtotal for the cart should be "([^"]*)"$/ do |subtotal|
@cart.reload
@cart.reload
@cart.subtotal.should eq(subtotal.to_f)
end

Expand All @@ -18,17 +18,52 @@
end

Then /^the total unique items on the cart should be "([^"]*)"$/ do |total|
@cart.reload
@cart.total_unique_items.should eq(total.to_i)
@cart.reload
@cart.total_unique_items.should eq(total.to_i)
end

When /^I remove (\d+) "([^"]*)" unit(s?) from cart$/ do |quantity, product_name, plural|
@cart.reload
@cart.reload
product = Product.find_by_name(product_name)
@cart.remove(product, quantity.to_i)
@cart.remove(product, quantity.to_i)
end

Then /^cart should be empty$/ do
@cart.reload
@cart.cart_items.should be_empty
@cart.reload
@cart.cart_items.should be_empty
end

Given /^I add (\d+) "([^"]*)" products to cart with price "([^"]*)"$/ do |quantity, product_name, price|
product = Product.find_by_name(product_name)
@cart.add(product, price.to_f, quantity.to_i)
end

Then /^the subtotal for "([^"]*)" on the cart should be "([^"]*)"$/ do |product_name, subtotal|
@cart.reload
product = Product.find_by_name(product_name)
@cart.subtotal_for(product).should eq(subtotal.to_f)
end

Then /^the quantity for "([^"]*)" on the cart should be "([^"]*)"$/ do |product_name, quantity|
@cart.reload
product = Product.find_by_name(product_name)
@cart.quantity_for(product).should eq(quantity.to_f)
end

Then /^the price for "([^"]*)" on the cart should be "([^"]*)"$/ do |product_name, price|
@cart.reload
product = Product.find_by_name(product_name)
@cart.price_for(product).should eq(price.to_f)
end

When /^I update the "([^"]*)" quantity to "([^"]*)"$/ do |product_name, quantity|
@cart.reload
product = Product.find_by_name(product_name)
@cart.update_quantity_for(product, quantity.to_i)
end

When /^I update the "([^"]*)" price to "([^"]*)"$/ do |product_name, price|
@cart.reload
product = Product.find_by_name(product_name)
@cart.update_price_for(product, price.to_f)
end
22 changes: 11 additions & 11 deletions lib/active_record/acts/shopping_cart/cart/instance_methods.rb
Expand Up @@ -23,7 +23,7 @@ def add(object, price, quantity = 1)
def remove(object, quantity = 1)
if cart_item = item_for(object)
if cart_item.quantity <= quantity
cart_item.delete
cart_item.delete
else
cart_item.quantity = (cart_item.quantity - quantity)
cart_item.save
Expand All @@ -35,20 +35,20 @@ def remove(object, quantity = 1)
# Returns the subtotal by summing the price times quantity for all the items in the cart
#
def subtotal
("%.2f" % cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
("%.2f" % cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
end

def shipping_cost
0
end
def shipping_cost
0
end

def taxes
subtotal * self.tax_pct * 0.01
end
def taxes
subtotal * self.tax_pct * 0.01
end

def tax_pct
8.25
end
def tax_pct
8.25
end

#
# Returns the total by summing the subtotal, taxes and shipping_cost
Expand Down
118 changes: 60 additions & 58 deletions lib/active_record/acts/shopping_cart/item/instance_methods.rb
@@ -1,66 +1,68 @@
module ActiveRecord
module Acts
module ShoppingCart
module Item
module InstanceMethods
module Acts
module ShoppingCart
module Item
module InstanceMethods

#
# Returns the cart item for the specified object
#
def item_for(object)
cart_items.where(:item_id => object.id).first
end
#
# Returns the cart item for the specified object
#
def item_for(object)
cart_items.where(:item_id => object.id).first
end

#
# Returns the subtotal of a specified item by multiplying the quantity times
# the price of the item.
#
def subtotal_for(object)
item = item_for(object)
if item
item.quantity * item.price
end
end
#
# Returns the subtotal of a specified item by multiplying the quantity times
# the price of the item.
#
def subtotal_for(object)
item = item_for(object)
if item
("%.2f" % (item.quantity * item.price)).to_f
else
0.0
end
end

#
# Returns the quantity of the specified object
#
def quantity_for(object)
item = item_for(object)
item ? item.quantity : 0
end
#
# Returns the quantity of the specified object
#
def quantity_for(object)
item = item_for(object)
item ? item.quantity : 0
end

#
# Updates the quantity of the specified object
#
def update_quantity_for(object, new_quantity)
item = item_for(object)
if item
item.quantity = new_quantity
item.save
end
end
#
# Updates the quantity of the specified object
#
def update_quantity_for(object, new_quantity)
item = item_for(object)
if item
item.quantity = new_quantity
item.save
end
end

#
# Returns the price of the specified object
#
def price_for(object)
item = item_for(object)
item ? item.price : 0
end
#
# Returns the price of the specified object
#
def price_for(object)
item = item_for(object)
item ? item.price : 0
end

#
# Updates the price of the specified object
#
def update_price_for(object, new_price)
item = item_for(object)
if item
item.price = new_price
item.save
end
end
end
end
end
end
#
# Updates the price of the specified object
#
def update_price_for(object, new_price)
item = item_for(object)
if item
item.price = new_price
item.save
end
end
end
end
end
end
end

This file was deleted.

6 changes: 3 additions & 3 deletions lib/active_record/acts/shopping_cart_item/instance_methods.rb
Expand Up @@ -4,7 +4,7 @@ module ShoppingCartItem
module InstanceMethods
#
# Returns the subtotal, multiplying the quantity times the price of the item.
#
#
def subtotal
self.quantity * self.price
end
Expand All @@ -16,7 +16,7 @@ def update_quantity(new_quantity)
self.quantity = new_quantity
self.save
end

#
# Updates the price of the item
#
Expand All @@ -27,4 +27,4 @@ def update_price(new_price)
end
end
end
end
end

0 comments on commit b72243d

Please sign in to comment.