Module: ActiveRecord::Acts::ShoppingCart::InstanceMethods

Defined in:
lib/active_record/acts/shopping_cart/cart_instance_methods.rb,
lib/active_record/acts/shopping_cart/item_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#add(object, price, quantity = 1) ⇒ Object

Adds a product to the cart



23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/acts/shopping_cart/cart_instance_methods.rb', line 23

def add(object, price, quantity = 1)
  cart_item = item_for(object)

  unless cart_item
    cart_items.create(:item => object, :price => price, :quantity => quantity)
  else
    cart_item.quantity = (cart_item.quantity + quantity)
    cart_item.save
  end
end

#delete(object) ⇒ Object

Delete an item from the cart



37
38
39
40
# File 'lib/active_record/acts/shopping_cart/cart_instance_methods.rb', line 37

def delete(object)
  cart_item = item_for(object)
  cart_items.delete(cart_item)
end

#item_for(object) ⇒ Object

Returns the cart item for the specified object



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

def item_for(object)
  cart_items.where(:item_id => object.id).first
end

#price_for(object) ⇒ Object

Returns the price of the specified object



46
47
48
49
# File 'lib/active_record/acts/shopping_cart/item_instance_methods.rb', line 46

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

#quantity_for(object) ⇒ Object

Returns the quantity of the specified object



27
28
29
30
# File 'lib/active_record/acts/shopping_cart/item_instance_methods.rb', line 27

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

#remove(object, quantity = 1) ⇒ Object

Remove an item from the cart



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/acts/shopping_cart/cart_instance_methods.rb', line 45

def remove(object, quantity = 1)
  cart_item = item_for(object)
  if cart_item
    if cart_item.quantity <= quantity
      cart_items.delete(cart_item)
    else
      cart_item.quantity = (cart_item.quantity - quantity)
      cart_item.save
    end
  end
end

#subtotalObject

Returns the subtotal by summing the price times quantity for all the items in the cart



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

def subtotal
  ("%.2f" % cart_items.sum("price * quantity")).to_f
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
21
22
# File 'lib/active_record/acts/shopping_cart/item_instance_methods.rb', line 17

def subtotal_for(object)
  item = item_for(object)
  if item
    item.quantity * item.price
  end
end

#totalObject

Returns the total by summing the subtotal, taxes and shipping_cost



16
17
18
# File 'lib/active_record/acts/shopping_cart/cart_instance_methods.rb', line 16

def total
  ("%.2f" % (subtotal + self.taxes + shipping_cost)).to_f
end

#total_unique_itemsObject

Return the number of unique items in the cart



60
61
62
# File 'lib/active_record/acts/shopping_cart/cart_instance_methods.rb', line 60

def total_unique_items
  cart_items.sum(:quantity)
end

#update_price_for(object, new_price) ⇒ Object

Updates the price of the specified object



54
55
56
57
58
59
60
# File 'lib/active_record/acts/shopping_cart/item_instance_methods.rb', line 54

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

#update_quantity_for(object, new_quantity) ⇒ Object

Updates the quantity of the specified object



35
36
37
38
39
40
41
# File 'lib/active_record/acts/shopping_cart/item_instance_methods.rb', line 35

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