mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-26 20:56:48 +00:00
These config values are relatively static but in some cases they can be called many times in the same request (like rendering a report or a large list of line_items in BOM). These values will now only get fetched from Redis/Postgres once at most per request/job.
30 lines
847 B
Ruby
30 lines
847 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Api
|
|
module Admin
|
|
# This serializer relies on `object` to respond to `#balance_value`. That's done in
|
|
# `CustomersWithBalanceQuery` due to the fact that ActiveRecord maps the DB result set's
|
|
# columns to instance methods. This way, the `balance_value` alias on that class ends up being
|
|
# `object.balance_value` here.
|
|
class CustomerWithBalanceSerializer < CustomerSerializer
|
|
attributes :balance, :balance_status
|
|
|
|
delegate :balance_value, to: :object
|
|
|
|
def balance
|
|
Spree::Money.new(balance_value, currency: CurrentConfig.get(:currency)).to_s
|
|
end
|
|
|
|
def balance_status
|
|
if balance_value.positive?
|
|
"credit_owed"
|
|
elsif balance_value.negative?
|
|
"balance_due"
|
|
else
|
|
""
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|