1

Does the money-rails gem require a specific order of price and currency when initializing an object? For example, take a look at the following:

Object.new(currency: 'JPY', price: 25)
=> #<Object id: nil, price_cents: 25, currency: "JPY">

If we specify the price first, we get an incorrect value (2500) for the price:

Object.new(price: 25, currency: 'JPY')
=> #<Object id: nil, price_cents: 2500, currency: "JPY">

Object contains the following: monetize :price_cents.

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57

1 Answers1

1

It looks like the order matters for certain currencies (including JPY because it doesn't have cents). This may not be the best solution but if anyone is stuck, here is what I did.

I added the following to the self.monetize method in money-rails to override the initialize methods for classes that use it:

define_method "initialize" do |opts = {}|
  opts = opts.deep_symbolize_keys
  opts = {currency: opts[:currency]}.merge(opts)
  super(opts)
end

This way it will send the currency first.

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57