Skip to content

Commit

Permalink
Rename ShoppingCart::Item::InstanceMethods module
Browse files Browse the repository at this point in the history
to ShoppingCart::Item
  • Loading branch information
David Padilla committed Mar 6, 2013
1 parent 98d5280 commit 559d673
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 67 deletions.
2 changes: 1 addition & 1 deletion lib/active_record/acts/shopping_cart.rb
Expand Up @@ -18,7 +18,7 @@ module ClassMethods
# #
def acts_as_shopping_cart_using(item_class) def acts_as_shopping_cart_using(item_class)
self.send :include, ActiveRecord::Acts::ShoppingCart::Collection self.send :include, ActiveRecord::Acts::ShoppingCart::Collection
self.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods self.send :include, ActiveRecord::Acts::ShoppingCart::Item
has_many :shopping_cart_items, :class_name => item_class.to_s.classify, has_many :shopping_cart_items, :class_name => item_class.to_s.classify,
:as => :owner, :dependent => :destroy :as => :owner, :dependent => :destroy
end end
Expand Down
56 changes: 56 additions & 0 deletions lib/active_record/acts/shopping_cart/item.rb
@@ -0,0 +1,56 @@
module ActiveRecord
module Acts
module ShoppingCart
module Item

#
# Returns the cart item for the specified object
#
def item_for(object)
shopping_cart_items.where(:item_id => object.id, :item_type => object.class.name).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)
item ? item.subtotal : 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)
item.update_quantity(new_quantity) if item
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)
item.update_price(new_price) if item
end
end
end
end
end
58 changes: 0 additions & 58 deletions lib/active_record/acts/shopping_cart/item/instance_methods.rb

This file was deleted.

7 changes: 2 additions & 5 deletions lib/acts_as_shopping_cart.rb
Expand Up @@ -6,11 +6,8 @@
module ActiveRecord module ActiveRecord
module Acts module Acts
module ShoppingCart module ShoppingCart
autoload :Collection, 'active_record/acts/shopping_cart/collection' autoload :Collection , 'active_record/acts/shopping_cart/collection'

autoload :Item , 'active_record/acts/shopping_cart/item'
module Item
autoload :InstanceMethods, 'active_record/acts/shopping_cart/item/instance_methods'
end
end end


module ShoppingCartItem module ShoppingCartItem
Expand Down
@@ -1,9 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '../../../../../spec_helper') require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')


describe ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods do describe ActiveRecord::Acts::ShoppingCart::Item do
let(:klass) do let(:klass) do
klass = Class.new klass = Class.new
klass.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods klass.send :include, ActiveRecord::Acts::ShoppingCart::Item
klass klass
end end


Expand Down

0 comments on commit 559d673

Please sign in to comment.