audited
audited copied to clipboard
Rails 6 Custom Audit model shows deprecation message in rspec
I've added a custom audit model like so in a Rails 6 initializer
Audited.config do |config|
config.audit_class = Audit
end
Running Rspec now gives the following deprecation message
bundle exec rspec
DEPRECATION WARNING: Initialization autoloaded the constant Audit.
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 Audit, for example,
the expected changes won't be reflected in that stale Module object.
These autoloaded constants have been unloaded.
Please, check the "Autoloading and Reloading Constants" guide for solutions.
(called from <top (required)> at /u/apps/someapp/config/environment.rb:5)
...Capybara starting Puma...
* Version 4.0.0 , codename: 4 Fast 4 Furious
The deprecation warning does not show in development or production when running rails s
I have the same problem here. Waiting for a solution =)
Thinking out loud about solutions, it might be that this needs to be configured as a string (which we call constantize
on later) or as a lambda/something more dynamic.
There is some discussion about a similar notice here.
There is some discussion about a similar notice here.
The suggestion here's to move it into lib/
and require
it so it doesn't get auto-reloaded. That's a good workaround, but maybe not ideal - we should be able to support auto-reloading of a custom audit class still.
I'm thinking about a String here as well. Could consider changing the config method to audit_class_name=
to mirror similar option naming in ActiveRecord.
https://github.com/fxn/zeitwerk#reloading would this help?
It seems like the way around this is to wrap it all in a Rails.application.config.to_prepare
block, like so:
Rails.application.config.to_prepare do
Audited.config do |config|
config.audit_class = CustomAudit
end
end
Solution derived from this issue comment: https://github.com/rails/rails/issues/36363#issuecomment-555192894