Permalink
Browse files

Rename ShoppingCart::Item::InstanceMethods module

to ShoppingCart::Item
  • Loading branch information...
1 parent 98d5280 commit 559d6739f0b34a2376b258fd0aeb3de4665d90fb @dabit dabit committed Mar 6, 2013
@@ -18,7 +18,7 @@ module ClassMethods
#
def acts_as_shopping_cart_using(item_class)
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,
:as => :owner, :dependent => :destroy
end
@@ -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
@@ -1,58 +0,0 @@
-module ActiveRecord
- module Acts
- module ShoppingCart
- module Item
- module InstanceMethods
-
- #
- # 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
-end
@@ -6,11 +6,8 @@
module ActiveRecord
module Acts
module ShoppingCart
- autoload :Collection, 'active_record/acts/shopping_cart/collection'
-
- module Item
- autoload :InstanceMethods, 'active_record/acts/shopping_cart/item/instance_methods'
- end
+ autoload :Collection , 'active_record/acts/shopping_cart/collection'
+ autoload :Item , 'active_record/acts/shopping_cart/item'
end
module ShoppingCartItem
@@ -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
klass = Class.new
- klass.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods
+ klass.send :include, ActiveRecord::Acts::ShoppingCart::Item
klass
end

0 comments on commit 559d673

Please sign in to comment.