Module: ActiveRecord::Acts::ShoppingCart::Item

Defined in:
lib/active_record/acts/shopping_cart/item.rb

Instance Method Summary collapse

Instance Method Details

#item_for(object) ⇒ Object

Returns the cart item for the specified object



9
10
11
# File 'lib/active_record/acts/shopping_cart/item.rb', line 9

def item_for(object)
  shopping_cart_items.where(item: object).first
end

#price_for(object) ⇒ Object

Returns the price of the specified object



41
42
43
44
# File 'lib/active_record/acts/shopping_cart/item.rb', line 41

def price_for(object)
  item = item_for(object)
  item ? item.price : 0
end

#quantity_for(object) ⇒ Object

Returns the quantity of the specified object



25
26
27
28
# File 'lib/active_record/acts/shopping_cart/item.rb', line 25

def quantity_for(object)
  item = item_for(object)
  item ? item.quantity : 0
end

#subtotal_for(object) ⇒ Object

Returns the subtotal of a specified item by multiplying the quantity times the price of the item.



17
18
19
20
# File 'lib/active_record/acts/shopping_cart/item.rb', line 17

def subtotal_for(object)
  item = item_for(object)
  item ? item.subtotal : 0
end

#update_price_for(object, new_price) ⇒ Object

Updates the price of the specified object



49
50
51
52
# File 'lib/active_record/acts/shopping_cart/item.rb', line 49

def update_price_for(object, new_price)
  item = item_for(object)
  item.update_price(new_price) if item
end

#update_quantity_for(object, new_quantity) ⇒ Object

Updates the quantity of the specified object



33
34
35
36
# File 'lib/active_record/acts/shopping_cart/item.rb', line 33

def update_quantity_for(object, new_quantity)
  item = item_for(object)
  item.update_quantity(new_quantity) if item
end