audited icon indicating copy to clipboard operation
audited copied to clipboard

Rails 6 Custom Audit model shows deprecation message in rspec

Open danlewis opened this issue 5 years ago • 7 comments

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

danlewis avatar Jul 07 '19 05:07 danlewis

I have the same problem here. Waiting for a solution =)

bpsimao avatar Jul 09 '19 14:07 bpsimao

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.

domcleal avatar Jul 11 '19 09:07 domcleal

There is some discussion about a similar notice here.

burmashave avatar Jul 11 '19 10:07 burmashave

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.

domcleal avatar Jul 11 '19 10:07 domcleal

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.

dbackeus avatar Nov 10 '19 17:11 dbackeus

https://github.com/fxn/zeitwerk#reloading would this help?

chase439 avatar Jun 07 '20 05:06 chase439

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

dmann avatar Aug 07 '20 14:08 dmann