From 49073faa9217b5b3115cfde5b0de072dd8271cd3 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 30 Jun 2019 23:50:38 +0200 Subject: adds two sections to the upgrading notes for autoloading [skip ci] --- guides/source/upgrading_ruby_on_rails.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'guides/source') diff --git a/guides/source/upgrading_ruby_on_rails.md b/guides/source/upgrading_ruby_on_rails.md index 54f014293d..4192063e9b 100644 --- a/guides/source/upgrading_ruby_on_rails.md +++ b/guides/source/upgrading_ruby_on_rails.md @@ -270,6 +270,16 @@ In that case, `app/models/concerns` is assumed to be a root directory (because i The `Concerns::` namespace worked with the classic autoloader as a side-effect of the implementation, but it was not really an intended behavior. An application using `Concerns::` needs to rename those classes and modules to be able to run in `zeitwerk` mode. +#### Having `app` in the autoload paths + +Some projects want something like `app/api/base.rb` to define `API::Base`, and add `app` to the autoload paths to accomplish that in `classic` mode. Since Rails adds all subdirectories of `app` to the autoload paths automatically, we have another situation in which there are nested root directories, so that setup no longer works. Similar principle we explained above with `concerns`. + +If you want to keep that structure, you'll need to delete the subdirectory from the autoload paths in an initializer: + +```ruby +ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/api") +``` + #### Autoloaded Constants and Explicit Namespaces If a namespace is defined in a file, as `Hotel` is here: @@ -391,6 +401,12 @@ To fix this, just remove the wildcards: config.autoload_paths << "#{config.root}/lib" ``` +#### Eager loading and autoloading are consistent + +In `classic` mode, if `app/models/foo.rb` defines `Bar`, you won't be able to autoload that file, but eager loading will work because it loads files recursively blindly. This can be a source of errors if you test things first eager loading, execution may fail later autoloading. + +In `zeitwerk` mode both loading modes are consistent, they fail and err in the same files. + #### How to Use the Classic Autoloader in Rails 6 Applications can load Rails 6 defaults and still use the classic autoloader by setting `config.autoloader` this way: -- cgit v1.2.3