Module: MoneyRails::Configuration

Included in:
MoneyRails
Defined in:
lib/money-rails/configuration.rb

Overview

MoneyRails configuration module. This is extended by MoneyRails to provide configuration settings.

Constant Summary collapse

@@include_validations =
true
@@amount_column =
{ postfix: '_cents', type: :integer, null: false, default: 0, present: true }
@@currency_column =
{ postfix: '_currency', type: :string, null: false, default: 'USD', present: true }
@@no_cents_if_whole =
nil
@@symbol =
nil
@@sign_before_symbol =
nil
@@default_format =
nil
@@raise_error_on_money_parsing =
false
@@preserve_user_input =
false

Instance Method Summary collapse

Instance Method Details

#configure {|_self| ... } ⇒ Object

Start a MoneyRails configuration block in an initializer.

example: Provide a default currency for the application

MoneyRails.configure do |config|
  config.default_currency = :eur
end

Yields:

  • (_self)

Yield Parameters:



16
17
18
# File 'lib/money-rails/configuration.rb', line 16

def configure
  yield self
end

#default_currencyObject

Configuration parameters



22
23
24
# File 'lib/money-rails/configuration.rb', line 22

def default_currency
  Money::Currency.new(Money.default_currency)
end

#default_currency=(currency_name) ⇒ Object

Set default currency of money library



27
28
29
30
# File 'lib/money-rails/configuration.rb', line 27

def default_currency=(currency_name)
  Money.default_currency = currency_name
  set_currency_column_for_default_currency!
end

#register_currency=(currency_options) ⇒ Object

Register a custom currency



33
34
35
# File 'lib/money-rails/configuration.rb', line 33

def register_currency=(currency_options)
  Money::Currency.register(currency_options)
end

#rounding_mode=(mode) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/money-rails/configuration.rb', line 42

def rounding_mode=(mode)
  valid_modes = [
    BigDecimal::ROUND_UP,
    BigDecimal::ROUND_DOWN,
    BigDecimal::ROUND_HALF_UP,
    BigDecimal::ROUND_HALF_DOWN,
    BigDecimal::ROUND_HALF_EVEN,
    BigDecimal::ROUND_CEILING,
    BigDecimal::ROUND_FLOOR
  ]
  raise ArgumentError, "#{mode} is not a valid rounding mode" unless valid_modes.include?(mode)
  Money.rounding_mode = mode
end

#set_currency_column_for_default_currency!Object



37
38
39
40
# File 'lib/money-rails/configuration.rb', line 37

def set_currency_column_for_default_currency!
  iso_code = default_currency.iso_code
  currency_column.merge! default: iso_code
end