You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/classic_to_zeitwerk_howto.md
+19-11Lines changed: 19 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ How to Activate `zeitwerk` Mode
52
52
53
53
### Applications running Rails 5.x or Less
54
54
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.
56
56
57
57
### Applications running Rails 6.x
58
58
@@ -164,24 +164,32 @@ All is good!
164
164
165
165
### Concerns
166
166
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
168
168
169
169
```
170
170
app/models
171
171
app/models/concerns
172
172
```
173
173
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`.
175
175
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:
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.
181
189
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.
183
191
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:
185
193
186
194
```ruby
187
195
# config/initializers/zeitwerk.rb
@@ -255,18 +263,18 @@ If the application reloads `Foo`, it will reload `Foo::InnerClass` too.
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.
0 commit comments