I have been getting the familiar DEPRECATION WARNING: Initialization autoloaded the constant <foo>
error message for one of my apps.
I have a Rails6 app that uses the dragonfly gem which comes with image resizing processors, and I use them for making thumbnails. I also have a custom processor used for making opengraph versions of images, and as such I need to pass in that configuration to dragonfly on start-up. The use of this specific gem is not strictly relevant, because it could be like any gem where it requires an initializer with a config block to set up on app boot.
The trimmed version of the initializer app/config/initializers/dragonfly.rb
:
Dragonfly.app.configure do
plugin :imagemagick
processor :open_graph, OpenGraphProcessor.new
# ... lots more configuration ...
end
The OpenGraphProcessor class lives within app/lib
.
I can easily resolve this using the standard approach:
Rails.application.reloader.to_prepare do
Dragonfly.app.configure do
plugin :imagemagick
processor :open_graph, OpenGraphProcessor.new
# ... lots more configuration ...
end
end
My question relates to whether there is a better/more canonical way of handling situations like this where initializers reference classes from within the app itself? Several of my initializers are basically now wrapped in Rails.application.reloader.to_prepare do ... end
yet it feels like a hack and perhaps I'm simply missing something.