Class: ShoppingCart::Order

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_order(user) ⇒ Object



42
43
44
# File 'app/models/shopping_cart/order.rb', line 42

def self.current_order(user)
  Order.find_by(state: 'in_progress', user: user)
end

Instance Method Details

#subtotal_priceObject



29
30
31
32
33
# File 'app/models/shopping_cart/order.rb', line 29

def subtotal_price
  order_items.inject(0) do |sum, item|
    sum + item.quantity * item.price
  end
end

#total_priceObject



35
36
37
38
39
40
# File 'app/models/shopping_cart/order.rb', line 35

def total_price
  subtotal = subtotal_price
  discount = discount_amount ? subtotal * (discount_amount.to_f / 100) : 0
  shipping_price ||= 0
  subtotal - shipping_price - discount
end