Class: MoneyAttribute::Railtie Private

Inherits:
Rails::Railtie
  • Object
show all
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.

Examples:

Generated initializer

MoneyAttribute.configure do |config|
  config.default_currency = 'BRL'
  config.added_currencies = [
    { currency: 'BTB', subunit: 8, symbol: '' }
  ]
end

Class Method Summary collapse

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.

Parameters:

  • fmt (Hash)

    the Rails currency format settings

Returns:

  • (String, Hash)

    a single format string or a per-sign format hash



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.

Parameters:

  • fmt (Hash)

    the Rails currency format settings

Returns:

  • (Hash)

    the :positive, :negative, and :zero format strings



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_formatHash

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.

Returns:

  • (Hash)

    the :decimal, :thousand, and :format keys for Mint



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.

Raises:

  • (ArgumentError)

    if a currency hash is missing a required key



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.message.include?('already registered')
      raise ArgumentError,
            "Invalid currency configuration: #{currency_data.inspect}. " \
            "Each currency must have :currency, :subunit, and :symbol keys. Error: #{e.message}"
    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.

Parameters:

  • str (String, nil)

    the Rails format string

Returns:

  • (String)

    the translated format string



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