Skip to content

Commit 63d7178

Browse files
committed
More docs on having app in the autoload paths
1 parent 3f66f24 commit 63d7178

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

guides/source/classic_to_zeitwerk_howto.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ If your application uses `Concerns` as namespace, you have two options:
212212

213213
Some projects want something like `app/api/base.rb` to define `API::Base`, and add `app` to the autoload paths to accomplish that.
214214

215-
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.
215+
Since Rails adds all subdirectories of `app` to the autoload paths automatically (with a few exceptions), 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.
216216

217217
However, you can keep that structure, just delete `app/api` from the autoload paths in an initializer:
218218

@@ -223,6 +223,22 @@ ActiveSupport::Dependencies.
223223
delete("#{Rails.root}/app/api")
224224
```
225225

226+
Beware of subdirectories that do not have files to be autoloaded/eager loaded. For example, if the application has `app/admin` with resources for [ActiveAdmin](https://activeadmin.info/), you need to ignore them. Same for `assets` and friends:
227+
228+
```ruby
229+
# config/initializers/zeitwerk.rb
230+
Rails.autoloaders.main.ignore(
231+
"app/admin",
232+
"app/assets",
233+
"app/javascripts",
234+
"app/views"
235+
)
236+
```
237+
238+
Without that configuration, the application would eager load those trees. Would err on `app/admin` because its files do not define constants, and would define a `Views` module, for example, as an unwanted side-effect.
239+
240+
As you see, having `app` in the autoload paths is technically possible, but a bit tricky.
241+
226242
### Autoloaded Constants and Explicit Namespaces
227243

228244
If a namespace is defined in a file, as `Hotel` is here:

0 commit comments

Comments
 (0)