Class: ShoppingCart::Order

Inherits:
ApplicationRecord show all
Includes:
AASM, CustomMethods
Defined in:
app/models/shopping_cart/order.rb

Instance Method Summary collapse

Methods included from CustomMethods

included

Instance Method Details

#add_item(id, type, quantity = 1) ⇒ Object



44
45
46
47
48
49
50
51
# File 'app/models/shopping_cart/order.rb', line 44

def add_item(id, type, quantity = 1)
  item = order_items.where("productable_id = ? AND productable_type = ?", id, type).first
  if item
    item.update_amount(quantity)
  else
    order_items.create(quantity: quantity, productable_id: id, productable_type: type)
  end
end

#destroy_if_orphantObject



63
64
65
# File 'app/models/shopping_cart/order.rb', line 63

def destroy_if_orphant
  self.destroy if order_items.count.zero?
end

#update_totalObject



53
54
55
56
57
58
59
60
61
# File 'app/models/shopping_cart/order.rb', line 53

def update_total
  return if errors.any?
  new_total = order_items.map(&:item_total).sum
  new_total -= (new_total * coupon.discount / 100) if coupon
  custom_steps.each do |step|
    new_total += self.try(step).try(:price) || 0
  end
  self.total = new_total
end