Permalink
Browse files

The road towards better specs

  • Loading branch information...
1 parent f1be812 commit 842c1688b376c12d00eb44cd4047d6b1319be4c4 @dabit dabit committed Sep 27, 2011
@@ -1,4 +1,10 @@
Feature: Shopping Cart
- Scenario: Add product to cart
- Given a product exists
+ Background:
+ Given a product "Apple" exists
+ And a shopping cart exists
+
+ Scenario: Totals
+ When I add product "Apple" to cart with price "99.99"
+ Then the subtotal for the cart should be "99.99"
+ Then the total for the cart should be "108.24"
@@ -1,3 +1,3 @@
-Given /^a product exists$/ do
- Product.create
+Given /^a product "([^"]*)" exists$/ do |name|
+ Product.create(:name => name)
end
@@ -0,0 +1,16 @@
+Given /^a shopping cart exists$/ do
+ @cart = ShoppingCart.create
+end
+
+Then /^the total for the cart should be "([^"]*)"$/ do |total|
+ @cart.total.should eq(total.to_f)
+end
+
+Then /^the subtotal for the cart should be "([^"]*)"$/ do |subtotal|
+ @cart.subtotal.should eq(subtotal.to_f)
+end
+
+When /^I add product "([^"]*)" to cart with price "([^"]*)"$/ do |product_name, price|
+ product = Product.find_by_name(product_name)
+ @cart.add(product, price)
+end
@@ -1,21 +1,24 @@
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
+ create_table :shopping_carts
create_table :shopping_cart_items do |t|
t.shopping_cart_item_fields
end
- create_table :products
+ create_table :products do |t|
+ t.string :name
+ end
end
class Product < ActiveRecord::Base
end
-# class ShoppingCart < ActiveRecord::Base
- # acts_as_shopping_cart
-# end
+class ShoppingCart < ActiveRecord::Base
+ acts_as_shopping_cart
+end
-# class ShoppingCartItem < ActiveRecord::Base
- # acts_as_shopping_cart_item
-# end
+class ShoppingCartItem < ActiveRecord::Base
+ acts_as_shopping_cart_item
+end
@@ -3,21 +3,6 @@ module Acts
module ShoppingCart
module Cart
module InstanceMethods
-
- #
- # Returns the subtotal by summing the price times quantity for all the items in the cart
- #
- def subtotal
- ("%.2f" % cart_items.sum("price * quantity")).to_f
- end
-
- #
- # Returns the total by summing the subtotal, taxes and shipping_cost
- #
- def total
- ("%.2f" % (subtotal + self.taxes + shipping_cost)).to_f
- end
-
#
# Adds a product to the cart
#
@@ -36,10 +21,9 @@ def add(object, price, quantity = 1)
# Remove an item from the cart
#
def remove(object, quantity = 1)
- cart_item = item_for(object)
- if cart_item
+ if cart_item = item_for(object)
if cart_item.quantity <= quantity
- cart_items.delete(cart_item)
+ cart_item.delete
else
cart_item.quantity = (cart_item.quantity - quantity)
cart_item.save
@@ -48,10 +32,36 @@ def remove(object, quantity = 1)
end
#
+ # 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
+ end
+
+ def shipping_cost
+ 0
+ end
+
+ def taxes
+ subtotal * self.tax_pct * 0.01
+ end
+
+ def tax_pct
+ 8.25
+ end
+
+ #
+ # Returns the total by summing the subtotal, taxes and shipping_cost
+ #
+ def total
+ ("%.2f" % (self.subtotal + self.taxes + self.shipping_cost)).to_f
+ end
+
+ #
# Return the number of unique items in the cart
#
def total_unique_items
- cart_items.sum(:quantity)
+ cart_items.inject(0) { |sum, item| sum += item.quantity }
end
end
end
Oops, something went wrong.

0 comments on commit 842c168

Please sign in to comment.