Class: Plutonium::UI::Display::Components::Currency

Inherits:
Phlexi::Display::Components::Base
  • Object
show all
Includes:
Phlexi::Display::Components::Concerns::DisplaysValue
Defined in:
lib/plutonium/ui/display/components/currency.rb

Overview

Renders a numeric value as currency (delimited, 2 decimals). The symbol is resolved by Currency.resolve_unit: an explicit unit: (a literal "£", a Symbol read off the record for per-row currencies, or false for no symbol) → the record's has_cents unit → default_currency_unit / the i18n number.currency.format.unit ($ in en).

display :price, as: :currency               # → configured/i18n default unit
display :price, as: :currency, unit: "£"
display :price, as: :currency, unit: :currency_symbol
display :price, as: :currency, unit: false  # no symbol

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_unitObject

The unit used when nothing more specific is configured. Returns the default_currency_unit config verbatim when set (including false to disable the symbol); otherwise the i18n number.currency.format.unit if the locale defines it, else no symbol. We don't hardcode a "$".



48
49
50
51
52
53
# File 'lib/plutonium/ui/display/components/currency.rb', line 48

def self.default_unit
  config = Plutonium.configuration.default_currency_unit
  return config unless config.nil?

  I18n.t("number.currency.format.unit", default: "")
end

.resolve_unit(explicit, record, key) ⇒ String

Resolves the currency unit string for a value, shared by this component and the grid/kanban Grid::Card so both format currency identically. Precedence, where nil means "not set, keep looking" and false means "explicitly no symbol, stop":

explicit unit → record's has_cents unit → configured/i18n default.

Parameters:

  • explicit (String, Symbol, false, nil)

    a per-display unit:.

  • record (Object)

    the record being rendered.

  • key (Symbol)

    the attribute name.

Returns:

  • (String)

    the unit to pass to number_to_currency ("" for none).



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plutonium/ui/display/components/currency.rb', line 32

def self.resolve_unit(explicit, record, key)
  unit = explicit
  unit = record.has_cents_unit_for(key) if unit.nil? && record.respond_to?(:has_cents_unit_for)
  unit = default_unit if unit.nil?

  case unit
  when nil, false then ""
  when Symbol then record.public_send(unit).to_s
  else unit.to_s
  end
end

Instance Method Details

#render_value(value) ⇒ Object



55
56
57
# File 'lib/plutonium/ui/display/components/currency.rb', line 55

def render_value(value)
  p(**attributes) { format_currency(value) }
end