Module: ActiveRecord::Acts::ShoppingCart::Collection

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

Instance Method Summary collapse

Instance Method Details

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

Adds a product to the cart



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 8

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

  if cart_item
    cumulative = cumulative == true ? cart_item.quantity : 0
    cart_item.quantity = (cumulative + quantity)
    cart_item.save
    cart_item
  else
    shopping_cart_items.create(item: object, price: price, quantity: quantity)
  end
end

#cart_itemsObject



95
96
97
98
99
100
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 95

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"

  shopping_cart_items
end

#clearObject

Deletes all shopping_cart_items in the shopping_cart



24
25
26
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 24

def clear
  shopping_cart_items.clear
end

#items?Boolean Also known as: has_items?

Returns true when the cart has items

Returns:

  • (Boolean)


31
32
33
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 31

def items?
  shopping_cart_items.any?
end

#no_items?Boolean Also known as: has_no_items?

Returns true when the cart is empty

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 39

def no_items?
  shopping_cart_items.empty?
end

#remove(object, quantity = 1) ⇒ Object

Remove an item from the cart



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 48

def remove(object, quantity = 1)
  cart_item = item_for(object)

  return unless cart_item

  if cart_item.quantity <= quantity
    cart_item.delete
  else
    cart_item.quantity = (cart_item.quantity - quantity)
    cart_item.save
  end
end

#shipping_costObject



69
70
71
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 69

def shipping_cost
  Money.new(0)
end

#subtotalObject

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



65
66
67
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 65

def subtotal
  shopping_cart_items.inject(Money.new(0)) { |a, e| a + (e.price * e.quantity) }
end

#tax_pctObject



77
78
79
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 77

def tax_pct
  8.25
end

#taxesObject



73
74
75
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 73

def taxes
  subtotal * tax_pct * 0.01
end

#totalObject

Returns the total by summing the subtotal, taxes and shipping_cost



84
85
86
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 84

def total
  subtotal + taxes + shipping_cost
end

#total_unique_itemsObject

Return the number of unique items in the cart



91
92
93
# File 'lib/active_record/acts/shopping_cart/collection.rb', line 91

def total_unique_items
  shopping_cart_items.map(&:quantity).sum
end