RubyMoney / money-rails

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

Rails 6 Autoload Deprecation Warning when using a lambda for `config.default_currency` #636

Closed clemens1483 closed 1 year ago

clemens1483 commented 3 years ago

Version: 1.14.0 Rails: 6.0.4

Hi, I am getting the rails 6 autoload deprecation warning

DEPRECATION WARNING: Initialization autoloaded the constants Tenant.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload Tenant, for example,
the expected changes won't be reflected in that stale Module object.

When I am using a model in a lambda in config.default_currency:

# config/initializers/money.rb

MoneyRails.configure do |config|
    config.default_currency = -> { Tenant.current.currency }

  ...
end

Currently the only work around I have found is to wrap the configure block in a ActiveSupport::Reloader.to_prepare block which is not ideal. From what I understand wrapping a class in a lambda in an initializer should usually not trigger the deprecation warning as the class is not loaded at that point unless the gem calls the lambda on initialization.

kholbekj commented 1 year ago

Why was this closed? This is broken in rails 7, where Money raises if passed a lambda with an unloaded model. Money seems to call the lambda too early.

semmons99 commented 1 year ago

it sat without someone submitting a PR for over a year. if it is important, someone will reopen with a PR to fix it.

kholbekj commented 1 year ago

Maybe though the feature should be removed from the readme, when there is no test and it doesn't work?

kholbekj commented 1 year ago

It seems like the issue is here: https://github.com/RubyMoney/money-rails/blob/93d2ee4ff610e85e89bd593923f0633ae974e66e/lib/money-rails/configuration.rb#L38 when determining the iso code, the config setter immediately evaluates the lambda, but in new rails world that isn't working.

I'm happy to make a PR to warn that this is unsupported in Rails 7, or to propose a fix, if you have an idea how to do so.

semmons99 commented 1 year ago

we would welcome a PR the warns RE: Rails 7