forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_serializer.rb
More file actions
70 lines (53 loc) · 1.55 KB
/
Copy pathorder_serializer.rb
File metadata and controls
70 lines (53 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true
module Api
class OrderSerializer < ActiveModel::Serializer
attributes :number, :completed_at, :total, :state, :shipment_state, :payment_state,
:outstanding_balance, :payments, :path, :cancel_path,
:changes_allowed, :changes_allowed_until, :item_count,
:shop_id, :paid
has_many :payments, serializer: Api::PaymentSerializer
# This method relies on `balance_value` as a computed DB column.
# See `CompleteOrdersWithBalanceQuery` for reference.
def outstanding_balance
-object.balance_value
end
def payments
object.payments.joins(:payment_method).where(state: %w(completed pending))
end
def shop_id
object.distributor_id
end
def item_count
object.line_items.sum(&:quantity)
end
def completed_at
object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: "%b %d, %Y %H:%M")
end
def changes_allowed_until
return I18n.t(:not_allowed) unless object.changes_allowed?
I18n.l(object.order_cycle&.orders_close_at, format: "%b %d, %Y %H:%M")
end
def shipment_state
object.shipment_state || nil
end
def payment_state
object.payment_state || nil
end
def state
object.state || nil
end
def path
order_path(object)
end
def cancel_path
return nil unless object.changes_allowed?
cancel_order_path(object)
end
def changes_allowed
object.changes_allowed?
end
def paid
object.paid?
end
end
end