Class: RightnowOms::Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/rightnow_oms/cart.rb

Instance Method Summary collapse

Instance Method Details

#add_item(cartable, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/rightnow_oms/cart.rb', line 22

def add_item(cartable, opts = {})
  quantity = opts[:quantity] || 1
  mergable = true if opts[:mergable].nil?

  return create_item(cartable, opts) unless mergable

  cart_item = cart_items.find_by_cartable(cartable)
  if cart_item
    cart_item.update_attributes(
      quantity: cart_item.quantity + quantity.to_i,
    )
  else
    cart_item = create_item(cartable, opts)
  end

  cart_item
end

#cartable_countObject



18
19
20
# File 'app/models/rightnow_oms/cart.rb', line 18

def cartable_count
  cart_items.roots.map.sum(&:quantity)
end

#totalObject



14
15
16
# File 'app/models/rightnow_oms/cart.rb', line 14

def total
  cart_items.roots.map.sum(&:total) || 0
end