Rails 5 disables autoloading after booting the app in production. With Rails 5, autoloading is now completely disabled if config.eager_load = true.
The concept of loading all the constants even before they are actually needed is called “Eager loading”. In a way it is opposite of “Autoloading”. In the case of “Autoloading” the application does not load the constant until it is needed. Once a class is needed and it is missing then the application starts looking in “autoloading paths” to load the missing class.
In my case I had a file called calendar.rb in the lib folder.
class Calendar < Struct.new(:view, :date, :callback)
...
end
and I was trying to use this class in a helper:
# config/application.rb
And now everything works fine
module CalendarHelper
include ActionView::Helpers::OutputSafetyHelper
include ActionView::Helpers::TagHelper
include ActionView::Context def calendar(date = Date.current, &block) Calendar.new(self, date, block).display end
...
end
In my application.rb I had next configuration:
config.autoload_paths += %W(#{config.root}/lib)
But when I was trying to call Calendar.new from my helper, I was getting an error. Strange errors regarding missing requires and dependencies.
ActionView::Template::Error (uninitialized constant CalendarHelper::Calendar):
So what I had to do was change the configuration
config.eager_load_paths << Rails.root.join('lib')
And now everything works fine
No comments:
Post a Comment