module CustomFields::Types::Money::Target

Protected Instance Methods

_check_money(names) click to toggle source
# File lib/custom_fields/types/money.rb, line 116
def _check_money(names)
  raise ArgumentError, 'Unrecognized amount' if [nil, ''].include? read_attribute.names[:cents_field]

  _get_money(names)
rescue StandardError
  errors.add(names[:name], $ERROR_INFO.to_s)
  false
end
_get_formatted_money(names) click to toggle source
# File lib/custom_fields/types/money.rb, line 136
def _get_formatted_money(names)
  _get_money(names).format(symbol: send(names[:allow_currency_from_symbol]),
                           no_cents_if_whole: true)
rescue StandardError
  nil
end
_get_money(names) click to toggle source
# File lib/custom_fields/types/money.rb, line 110
def _get_money(names)
  _set_money_defaults(names)
  ::Money.new(read_attribute(names[:cents_field]),
              read_attribute(names[:currency_field]) || ::Money.default_currency)
end
_set_money(_money, names) click to toggle source
# File lib/custom_fields/types/money.rb, line 125
def _set_money(_money, names)
  return if _money.blank?

  _set_money_defaults(names)
  money = _money.is_a?(Money) ? _money : ::Monetize.parse(_money)
  write_attribute(names[:cents_field], money.cents)
  write_attribute(names[:currency_field], money.currency.iso_code)
rescue StandardError
  errors.add(names[:name], $ERROR_INFO.to_s)
end
_set_money_defaults(names) click to toggle source
# File lib/custom_fields/types/money.rb, line 105
def _set_money_defaults(names)
  ::Monetize.assume_from_symbol = send(names[:allow_currency_from_symbol])
  ::Money.default_currency = send(names[:default_currency])
end