Class: Plutonium::UI::Display::Components::Currency
- Inherits:
-
Phlexi::Display::Components::Base
- Object
- Phlexi::Display::Components::Base
- Plutonium::UI::Display::Components::Currency
- 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
-
.default_unit ⇒ Object
The unit used when nothing more specific is configured.
-
.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.
Instance Method Summary collapse
Class Method Details
.default_unit ⇒ Object
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.
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 |