Skip to content

Commit 0a13dc8

Browse files
committed
Even more edits to the classic migration guide
1 parent a9adf93 commit 0a13dc8

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

guides/source/classic_to_zeitwerk_howto.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ How to Activate `zeitwerk` Mode
5252

5353
### Applications running Rails 5.x or Less
5454

55-
In applications running a Rails version previous to 6.0, `zeitwerk` mode is not available. You need to be in at least Rails 6.0.
55+
In applications running a Rails version previous to 6.0, `zeitwerk` mode is not available. You need to be at least in Rails 6.0.
5656

5757
### Applications running Rails 6.x
5858

@@ -164,24 +164,32 @@ All is good!
164164

165165
### Concerns
166166

167-
You can autoload and eager load from a standard structure like
167+
You can autoload and eager load from a standard structure with `concerns` subdirectories like
168168

169169
```
170170
app/models
171171
app/models/concerns
172172
```
173173

174-
In that case, `app/models/concerns` is assumed to be a root directory (because it belongs to the autoload paths), and it is ignored as namespace. So, `app/models/concerns/foo.rb` should define `Foo`, not `Concerns::Foo`.
174+
By default, `app/models/concerns` belongs to the autoload paths and therefore it is assumed to be a root directory. So, by default, `app/models/concerns/foo.rb` should define `Foo`, not `Concerns::Foo`.
175175

176-
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.
176+
If your application uses `Concerns` as namespace, you have two options:
177+
178+
1. Remove the `Concerns` namespace from those classes and modules and update client code.
179+
2. Leave things as they are by removing `app/models/concerns` from the autoload paths:
180+
181+
```ruby
182+
# config/initializers/zeitwerk.rb
183+
ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/models/concerns")
184+
```
177185

178186
### Having `app` in the autoload paths
179187

180-
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.
188+
Some projects want something like `app/api/base.rb` to define `API::Base`, and add `app` to the autoload paths to accomplish that.
181189

182-
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`.
190+
Since Rails adds all subdirectories of `app` to the autoload paths automatically (with a few exceptions like directories for assets), we have another situation in which there are nested root directories, similar to what happens with `app/models/concerns`. That setup no longer works as is.
183191

184-
If you want to keep that structure, you'll need to delete the subdirectory from the autoload paths in an initializer:
192+
However, you can keep that structure, just delete `app/api` from the autoload paths in an initializer:
185193

186194
```ruby
187195
# config/initializers/zeitwerk.rb
@@ -255,18 +263,18 @@ If the application reloads `Foo`, it will reload `Foo::InnerClass` too.
255263

256264
### Globs in `config.autoload_paths`
257265

258-
Beware of configurations like
266+
Beware of configurations that use wildcards like
259267

260268
```ruby
261-
config.autoload_paths += Dir["#{config.root}/lib/**/"]
269+
config.autoload_paths += Dir["#{config.root}/extras/**/"]
262270
```
263271

264-
Every element of `config.autoload_paths` should represent the top-level namespace (`Object`) and they cannot be nested in consequence (with the exception of `concerns` directories explained above).
272+
Every element of `config.autoload_paths` should represent the top-level namespace (`Object`). That won't work.
265273

266274
To fix this, just remove the wildcards:
267275

268276
```ruby
269-
config.autoload_paths << "#{config.root}/lib"
277+
config.autoload_paths << "#{config.root}/extras"
270278
```
271279

272280
### Spring and the `test` Environment

0 commit comments

Comments
 (0)