Skip to content

Commit 3f66f24

Browse files
committed
More docs about Zeitwerk compliance
1 parent 237f9c8 commit 3f66f24

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

guides/source/classic_to_zeitwerk_howto.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,26 @@ If that prints `true`, `zeitwerk` mode is enabled.
9898
Does my Application Comply with Zeitwerk Conventions?
9999
-----------------------------------------------------
100100

101-
Once `zeitwerk` mode is enabled, please run:
101+
### config.eager_load_paths
102+
103+
Compliance test runs only for eager loaded files. Therefore, in order to verify Zeitwerk compliance, it is recommended to have all autoload paths in the eager load paths.
104+
105+
This is already the case by default, but if the project has custom autoload paths configured just like this:
106+
107+
```ruby
108+
config.autoload_paths << "#{Rails.root}/extras"
109+
```
110+
111+
those are not eager loaded and won't be verified. Adding them to the eager load paths is easy:
112+
113+
```ruby
114+
config.autoload_paths << "#{Rails.root}/extras"
115+
config.eager_load_paths << "#{Rails.root}/extras"
116+
```
117+
118+
### zeitwerk:check
119+
120+
Once `zeitwerk` mode is enabled and the configuration of eager load paths double-checked, please run:
102121

103122
```
104123
bin/rails zeitwerk:check
@@ -114,7 +133,9 @@ All is good!
114133

115134
There can be additional output depending on the application configuration, but the last "All is good!" is what you are looking for.
116135

117-
If there's any file that does not define the expected constant, the task will tell you. It does so one file at a time, because if it moved on, the failure loading one file could cascade into other failures unrelated to the check we want to run and the error report would be confusing.
136+
If the double-check explained in the previous section determined actually there have to be some custom autoload paths outside the eager load paths, the task will detect and warn about them. However, if the test suite loads those files successfully, you're good.
137+
138+
Now, if there's any file that does not define the expected constant, the task will tell you. It does so one file at a time, because if it moved on, the failure loading one file could cascade into other failures unrelated to the check we want to run and the error report would be confusing.
118139

119140
If there's one constant reported, fix that particular one and run the task again. Repeat until you get "All is good!".
120141

@@ -145,15 +166,15 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
145166
end
146167
```
147168

148-
Doing so affects how Active Support inflects globally. That may be fine, but if you prefer you can also pass overrides to the inflector used by the autoloader:
169+
Doing so affects how Active Support inflects globally. That may be fine, but if you prefer you can also pass overrides to the inflectors used by the autoloaders:
149170

150171
```ruby
151172
# config/initializers/zeitwerk.rb
152-
Rails.autoloaders.each do |autoloader|
153-
autoloader.inflector.inflect("vat" => "VAT")
154-
end
173+
Rails.autoloaders.main.inflector.inflect("vat" => "VAT")
155174
```
156175

176+
With this option you have more control, because only files called exactly `vat.rb` or directories exactly called `vat` will be inflected as `VAT`. A file called `vat_rules.rb` is not affected by that and can define `VatRules` just fine. This may be handy if the project has this kind of naming inconsistencies.
177+
157178
With that in place, the check passes!
158179

159180
```

0 commit comments

Comments
 (0)