Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoloading and Zeitwerk Mode in Rails 6

Is any of this code depreciated in Zeitwerk Mode in Rails 6?

 class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # config/application.rb
    config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}")]
    config.i18n.fallbacks = true

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
    config.autoload_paths += ["#{config.root}/app/queries/"]


    # https://gist.github.com/maxim/6503591 (should remove this and fix)
    config.eager_load_paths << Rails.root.join("lib")

I read that autoloading is being removed, and so far it has not caused an issue but wanted to remove deprecated code. If it is depreciated, how do I load my code?

like image 713
user2012677 Avatar asked Dec 27 '19 19:12

user2012677


People also ask

What is Zeitwerk mode in rails?

Starting with Rails 6, Rails ships with a new and better way to autoload, which delegates to the Zeitwerk gem. This is zeitwerk mode. By default, applications loading the 6.0 and 6.1 framework defaults run in zeitwerk mode, and this is the only mode available in Rails 7.

What is autoloading in Rails?

Rails automatically reloads classes and modules if application files in the autoload paths change. More precisely, if the web server is running and application files have been modified, Rails unloads all autoloaded constants managed by the main autoloader just before the next request is processed.

What does Zeitwerk mean?

The ZEITWERK is the first mechanical wristwatch that displays hours and minutes with jumping numerals. Its trailblazing design expresses the innovative concept.


1 Answers

Nothing you have here is deprecated, however one thing worth mentioning is from the documentation:

The array of autoload paths can be extended by mutating config.autoload_paths, in config/application.rb, but nowadays this is discouraged.

Rails 5+ is discouraging the use of manually extending config.autoload_paths because of potential issues it can cause in your production environment. The discussion dates back to 2013 and you can read about it here.

From Rails 5+ all directories under app/ are autoloaded by default. If you'd like to follow the Rails recommendation, you should remove this line

config.autoload_paths += ["#{config.root}/app/queries/"]

and move your queries directory under "#{Rails.root}/app" folder.

like image 132
Hen Avatar answered Sep 18 '22 21:09

Hen