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
+23-7Lines changed: 23 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,9 +29,9 @@ Starting with Rails 6, Rails ships with a new and better way to autoload, which
29
29
Why Switch from `classic` to `zeitwerk`?
30
30
----------------------------------------
31
31
32
-
The `classic` autoloader has been extremely useful, but had a number of [issues](https://guides.rubyonrails.org/v6.1/autoloading_and_reloading_constants_classic_mode.html#common-gotchas) that made autoloading a bit tricky and confusing at times. Zeitwerk was developed to address them, among other [motivations](https://github.com/fxn/zeitwerk#motivation).
32
+
The `classic` autoloader has been extremely useful, but had a number of [issues](https://guides.rubyonrails.org/v6.1/autoloading_and_reloading_constants_classic_mode.html#common-gotchas) that made autoloading a bit tricky and confusing at times. Zeitwerk was developed to address this, among other [motivations](https://github.com/fxn/zeitwerk#motivation).
33
33
34
-
When upgrading to Rails 6.x, it is highly encouraged to switch to `zeitwerk` mode because `classic` mode is deprecated.
34
+
When upgrading to Rails 6.x, it is highly encouraged to switch to `zeitwerk` mode because it is a better autoloader, `classic` mode is deprecated.
35
35
36
36
Rails 7 ends the transition period and does not include `classic` mode.
37
37
@@ -80,7 +80,7 @@ config.autoloader = :zeitwerk
80
80
81
81
In Rails 7 there is only `zeitwerk` mode, you do not need to do anything to enable it.
82
82
83
-
Indeed, the setter `config.autoloader=` does not even exist. If `config/application.rb`has it, please just delete the line.
83
+
Indeed, in Rails 7 the setter `config.autoloader=` does not even exist. If `config/application.rb`uses it, please delete the line.
84
84
85
85
86
86
How to Verify The Application Runs in `zeitwerk` Mode?
@@ -162,6 +162,8 @@ Hold on, I am eager loading the application.
162
162
All is good!
163
163
```
164
164
165
+
Once all is good, it is recommended to keep validating the project in the test suite. The section [_Check Zeitwerk Compliance in the Test Suite_](#check-zeitwerk-compliance-in-the-test-suite) explains how to do this.
166
+
165
167
### Concerns
166
168
167
169
You can autoload and eager load from a standard structure with `concerns` subdirectories like
@@ -306,7 +308,7 @@ Please make sure to depend on at least Bootsnap 1.4.4.
306
308
Check Zeitwerk Compliance in the Test Suite
307
309
-------------------------------------------
308
310
309
-
The task `zeitwerk:check` is handy while migrating. Once the project is compliant, it is recommended to automate this check. In order to do so, it is enough to eager load the application, which is all the task does, indeed.
311
+
The task `zeitwerk:check` is handy while migrating. Once the project is compliant, it is recommended to automate this check. In order to do so, it is enough to eager load the application, which is all `zeitwerk:check` does, indeed.
310
312
311
313
### Continuous Integration
312
314
@@ -349,6 +351,21 @@ RSpec.describe "Zeitwerk compliance" do
349
351
end
350
352
```
351
353
354
+
Delete any `require` calls
355
+
--------------------------
356
+
357
+
In my experience, projects generally do not do this. But I've seen a couple, and have heard of a few others.
358
+
359
+
In Rails application you use `require` exclusively to load code from `lib` or from 3rd party like gem dependencies or the standard library. **Never load autoloadable application code with `require`**. See why this was a bad idea already in `classic`[here](https://guides.rubyonrails.org/v6.1/autoloading_and_reloading_constants_classic_mode.html#autoloading-and-require).
360
+
361
+
```ruby
362
+
require"nokogiri"# GOOD
363
+
require"net/http"# GOOD
364
+
require"user"# BAD, DELETE THIS (assuming app/models/user.rb)
365
+
```
366
+
367
+
Please delete any `require` calls of that type.
368
+
352
369
New Features You Can Leverage
353
370
-----------------------------
354
371
@@ -358,13 +375,12 @@ All known use cases of `require_dependency` have been eliminated with Zeitwerk.
358
375
359
376
If your application uses Single Table Inheritance, please see the [Single Table Inheritance section](autoloading_and_reloading_constants.html#single-table-inheritance) of the Autoloading and Reloading Constants (Zeitwerk Mode) guide.
360
377
361
-
362
378
### Qualified Names in Class and Module Definitions Are Now Possible
363
379
364
380
You can now robustly use constant paths in class and module definitions:
365
381
366
382
```ruby
367
-
# Autoloading in this class' body matches Ruby semantics now.
383
+
# Autoloading in this class body matches Ruby semantics now.
0 commit comments