Class: Corzinus::Order
Class Method Summary
collapse
Instance Method Summary
collapse
#addresses, #any_address?
Class Method Details
.assm_states ⇒ Object
45
46
47
|
# File 'app/models/corzinus/order.rb', line 45
def self.assm_states
aasm.states.map(&:name)
end
|
.not_empty ⇒ Object
49
50
51
52
53
|
# File 'app/models/corzinus/order.rb', line 49
def self.not_empty
joins(:order_items)
.group('corzinus_orders.id')
.having('COUNT(corzinus_order_items) != 0')
end
|
Instance Method Details
#access_deliveries ⇒ Object
55
56
57
58
59
|
# File 'app/models/corzinus/order.rb', line 55
def access_deliveries
address = send(Address::DELIVERY)
return unless address
@access_deliveries ||= Delivery.where(country: address.country)
end
|
#add_item!(productable, quantity = 1) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'app/models/corzinus/order.rb', line 61
def add_item!(productable, quantity = 1)
return if quantity.zero?
item = order_items.find_by(productable: productable)
if item
item.increment! :quantity, quantity
else
order_items.create(quantity: quantity, productable: productable)
end
end
|
#calc_total_cost(*additions) ⇒ Object
97
98
99
|
# File 'app/models/corzinus/order.rb', line 97
def calc_total_cost(*additions)
sub_total + additions.map { |addition| send("#{addition}_cost") }.sum
end
|
#coupon_cost ⇒ Object
89
90
91
|
# File 'app/models/corzinus/order.rb', line 89
def coupon_cost
coupon ? coupon.calc_discount(sub_total) : 0.00
end
|
#delivery_cost ⇒ Object
93
94
95
|
# File 'app/models/corzinus/order.rb', line 93
def delivery_cost
delivery ? delivery.price : 0.00
end
|
#items_count ⇒ Object
81
82
83
|
# File 'app/models/corzinus/order.rb', line 81
def items_count
order_items.map(&:quantity).sum
end
|
#merge_order!(order) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'app/models/corzinus/order.rb', line 71
def merge_order!(order)
return self if self == order
order.order_items.each do |order_item|
add_item!(order_item.productable, order_item.quantity).save
end
self.coupon = nil if order.coupon.present?
order.destroy && order.coupon&.update_attributes(order: self)
tap(&:save)
end
|
#sub_total ⇒ Object
85
86
87
|
# File 'app/models/corzinus/order.rb', line 85
def sub_total
order_items.map(&:sub_total).sum
end
|