Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that order of attributes matters when expect_whole_subunits is enabled #708

Open
tagliala opened this issue Nov 22, 2024 · 0 comments

Comments

@tagliala
Copy link
Contributor

tagliala commented Nov 22, 2024

Hello,

we are in a situation where we want to take advantage of Monetize parser to deal with . and , (ref: #701)

It is worth to report that the order of fields matters because how the parser works

The parser expects that the currency is being already set when parse the amount, and this makes the order of assignment important, creating issues with currencies with a number of decimal points different from 2

Example:

# initializer
Monetize.expect_whole_subunits = true

MoneyRails.configure do |config|
  # Should not matter, added just to remove the deprecations
  config.rounding_mode = BigDecimal::ROUND_HALF_DOWN 
  config.locale_backend = :i18n
end

# Model
class Product < ApplicationRecord
  monetize :price_cents
end

Result

> Product.new(price_currency: 'JOD', price: '1.000').price
=> #<Money fractional:1000 currency:JOD>

Product.new(price: '1.000', price_currency: 'JOD').price
=> #<Money fractional:1000000 currency:JOD>

Solution

In rails, the order of permitted_params will matter, so you want to have something like this:

params.require(:product).permit(:price_currency, :price)
> params.require(:product).permit(:price, :price_currency)
#<ActionController::Parameters {"price"=>"123", "price_currency"=>"USD"} permitted: true>

> params.require(:product).permit(:price_currency, :price)
#<ActionController::Parameters {"price_currency"=>"USD", "price"=>"123"} permitted: true>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant