RubyMoney / money-rails

Integration of RubyMoney - Money with Rails
MIT License
1.8k stars 387 forks source link

[FEATURE REQUEST] monetize with_model_rounding_mode #694

Closed asavageiv closed 6 months ago

asavageiv commented 7 months ago

Our company operates in multiple countries performing transactions with Money with different rounding modes per country. Right now, our code is littered with blocks like this:

Money.with_rounding_mode(country.rounding_mode) do
  money_instance.format # or some other money calculation
end

This is error prone and tedious. It's easy to forget to add the block everywhere and it's not safe for async code because with_rounding_mode uses Thread.current.

I think a solution to this would be to have per-instance rounding modes on Money objects and support for with_model_rounding_mode on monetize.

It could be an error to perform an operation on two values with different rounding modes.

What do you think about this approach? Is there a better one?

semmons99 commented 6 months ago

I think this works better in money as you suggested in that repo. I welcome either PR