Class: MoneyAttribute::Railtie Private
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- MoneyAttribute::Railtie
- Defined in:
- lib/money_attribute/railtie.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Rails engine integration.
On boot, includes the migration and form-builder extensions into the corresponding Rails classes, wires Mint's locale backend to Rails i18n, and registers custom currencies declared in the initializer.
Class Method Summary collapse
-
.build_format(fmt) ⇒ String, Hash
private
Builds the final currency format string or hash for Mint.
-
.build_hash_format(fmt) ⇒ Hash
private
Builds a per-sign currency format hash.
-
.build_locale_format ⇒ Hash
private
Builds the locale-aware currency formatting hash.
-
.register_custom_currencies! ⇒ void
private
Registers custom currencies configured by the application.
-
.setup_locale_backend! ⇒ void
private
Configures Mint to use the Rails locale currency format.
-
.translate_format(str) ⇒ String
private
Translates Rails currency placeholders into Mint placeholders.
Class Method Details
.build_format(fmt) ⇒ String, Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds the final currency format string or hash for Mint.
60 61 62 63 64 65 66 |
# File 'lib/money_attribute/railtie.rb', line 60 def self.build_format(fmt) if %i[positive negative zero].any? { |k| fmt.key?(k) } build_hash_format(fmt) else translate_format(fmt[:format]) end end |
.build_hash_format(fmt) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a per-sign currency format hash.
73 74 75 76 77 78 79 |
# File 'lib/money_attribute/railtie.rb', line 73 def self.build_hash_format(fmt) { positive: translate_format(fmt[:positive] || fmt[:format]), negative: translate_format(fmt[:negative] || fmt[:format]), zero: translate_format(fmt[:zero] || fmt[:format]) } end |
.build_locale_format ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds the locale-aware currency formatting hash.
50 51 52 53 |
# File 'lib/money_attribute/railtie.rb', line 50 def self.build_locale_format fmt = I18n.t('number.currency.format', default: {}) { decimal: fmt[:separator], thousand: fmt[:delimiter], format: build_format(fmt) } end |
.register_custom_currencies! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers custom currencies configured by the application.
Accepts hashes with :currency, :subunit, :symbol keys or the
matching positional array form. Already-registered currencies are skipped.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/money_attribute/railtie.rb', line 98 def self.register_custom_currencies! Array(MoneyAttribute.config.added_currencies).each do |currency_data| if currency_data.respond_to?(:values_at) code = currency_data[:currency] subunit = currency_data[:subunit] symbol = currency_data[:symbol] else code, subunit, symbol = *currency_data end Money::Currency.register(code:, subunit:, symbol:) rescue KeyError => e unless e..include?('already registered') raise ArgumentError, "Invalid currency configuration: #{currency_data.inspect}. " \ "Each currency must have :currency, :subunit, and :symbol keys. Error: #{e.}" end end end |
.setup_locale_backend! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Configures Mint to use the Rails locale currency format.
42 43 44 |
# File 'lib/money_attribute/railtie.rb', line 42 def self.setup_locale_backend! ::Mint.locale_backend = method(:build_locale_format).to_proc end |
.translate_format(str) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Translates Rails currency placeholders into Mint placeholders.
86 87 88 |
# File 'lib/money_attribute/railtie.rb', line 86 def self.translate_format(str) str.to_s.gsub('%n', '%<amount>f').gsub('%u', '%<symbol>s') end |