RubyMoney / money-rails

Integration of RubyMoney - Money with Rails
MIT License
1.79k stars 386 forks source link

Wrong currency is ignored when not using `_currency` attribute #676

Closed TastyPi closed 6 months ago

TastyPi commented 1 year ago

When a model does not have a currency attribute, monetize does not check whether the value passed to amount= uses the correct currency.

e.g.

class Payment < ApplicationRecord
  monetize :amount_cents, with_currency: :usd
end

p = Payment.new
p.amount = Money.from_cents(100, :eur)
puts p.valid? # true
puts p.amount.inspect # #<Money fractional:100.0 currency:USD>

The currency is silently converted from EUR to USD with no error.

TastyPi commented 1 year ago

Ah, it turns out there is an option MoneyRails.raise_error_on_money_parsing that needs to be set to true, but is undocumented.

terryyin commented 7 months ago

The generated setter by monetize still silently ignore a currency if not found and use the default value. And it won't raise any error even if setting MoneyRails.raise_error_on_money_parsing.