Skip to content

Commit

Permalink
Renamed the use of ShoppingCart#cart_items to shopping_cart_items to …
Browse files Browse the repository at this point in the history
…follow convention
  • Loading branch information
David Padilla committed Sep 30, 2011
1 parent 9d3db77 commit 633dc25
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.markdown
Expand Up @@ -54,6 +54,11 @@ In order for this to work, the Shopping Cart Item model should have the followin
t.shopping_cart_item_fields # Creates the cart items fields
end

### Shopping Cart Items

Your ShoppingCart class will have a _shopping_cart_items_ association
that returns all the ShoppingCartItem objects in your cart.

### Add Items

To add an item to the cart you use the add method. You have to send the object and the price of the object as parameters.
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/shopping_cart_steps.rb
Expand Up @@ -30,7 +30,7 @@

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

Given /^I add (\d+) "([^"]*)" products to cart with price "([^"]*)"$/ do |quantity, product_name, price|
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/acts/shopping_cart.rb
Expand Up @@ -19,7 +19,7 @@ module ClassMethods
def acts_as_shopping_cart_using(item_class)
self.send :include, ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods
self.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods
has_many :cart_items, :class_name => item_class.to_s.classify, :as => :owner
has_many :shopping_cart_items, :class_name => item_class.to_s.classify, :as => :owner
end

#
Expand Down
11 changes: 8 additions & 3 deletions lib/active_record/acts/shopping_cart/cart/instance_methods.rb
Expand Up @@ -10,7 +10,7 @@ def add(object, price, quantity = 1)
cart_item = item_for(object)

unless cart_item
cart_items.create(:item => object, :price => price, :quantity => quantity)
shopping_cart_items.create(:item => object, :price => price, :quantity => quantity)
else
cart_item.quantity = (cart_item.quantity + quantity)
cart_item.save
Expand All @@ -35,7 +35,7 @@ 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" % shopping_cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
end

def shipping_cost
Expand All @@ -61,7 +61,12 @@ def total
# Return the number of unique items in the cart
#
def total_unique_items
cart_items.inject(0) { |sum, item| sum += item.quantity }
shopping_cart_items.inject(0) { |sum, item| sum += item.quantity }
end

def cart_items
warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart, please use ShoppingCart#shopping_cart_items instead"
self.shopping_cart_items
end
end
end
Expand Down
Expand Up @@ -8,7 +8,7 @@ module InstanceMethods
# Returns the cart item for the specified object
#
def item_for(object)
cart_items.where(:item_id => object.id).first
shopping_cart_items.where(:item_id => object.id).first
end

#
Expand Down
Expand Up @@ -9,7 +9,7 @@

let(:subject) do
subject = klass.new
subject.stub(:cart_items).and_return([])
subject.stub(:shopping_cart_items).and_return([])
subject
end

Expand All @@ -26,7 +26,7 @@
end

it "creates a new shopping cart item" do
subject.cart_items.should_receive(:create).with(:item => object, :price => 19.99, :quantity => 3)
subject.shopping_cart_items.should_receive(:create).with(:item => object, :price => 19.99, :quantity => 3)
subject.add(object, 19.99, 3)
end
end
Expand Down Expand Up @@ -78,7 +78,7 @@
describe :subtotal do
context "cart has no items" do
before do
subject.stub(:cart_items).and_return([])
subject.stub(:shopping_cart_items).and_return([])
end

it "returns 0" do
Expand All @@ -89,7 +89,7 @@
context "cart has items" do
before do
items = [stub(:quantity => 2, :price => 33.99), stub(:quantity => 1, :price => 45.99)]
subject.stub(:cart_items).and_return(items)
subject.stub(:shopping_cart_items).and_return(items)
end

it "returns the sum of the price * quantity for all items" do
Expand Down Expand Up @@ -144,7 +144,7 @@
context "cart has some items" do
before do
items = [stub(:quantity => 2, :price => 33.99), stub(:quantity => 1, :price => 45.99)]
subject.stub(:cart_items).and_return(items)
subject.stub(:shopping_cart_items).and_return(items)
end

it "returns the sum of the quantities of all shopping cart items" do
Expand Down
Expand Up @@ -7,11 +7,11 @@
klass
end

let(:cart_items) { stub }
let(:shopping_cart_items) { stub }

let(:subject) do
subject = klass.new
subject.stub(:cart_items).and_return(cart_items)
subject.stub(:shopping_cart_items).and_return(shopping_cart_items)
subject
end

Expand All @@ -21,7 +21,7 @@
describe :item_for do
context "no cart item exists for the object" do
before do
cart_items.should_receive(:where).with(:item_id => object.id).and_return([])
shopping_cart_items.should_receive(:where).with(:item_id => object.id).and_return([])
end

it "returns the shopping cart item object for the requested object" do
Expand All @@ -31,7 +31,7 @@

context "a cart item exists for the object" do
before do
cart_items.should_receive(:where).with(:item_id => object.id).and_return([ item ])
shopping_cart_items.should_receive(:where).with(:item_id => object.id).and_return([ item ])
end

it "returns that item" do
Expand Down

0 comments on commit 633dc25

Please sign in to comment.