Skip to content

Commit 30653cc

Browse files
committed
Edit pass in the C2Z HOWTO guide
1 parent ac1d5f0 commit 30653cc

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

guides/source/classic_to_zeitwerk_howto.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Starting with Rails 6, Rails ships with a new and better way to autoload, which
2929
Why Switch from `classic` to `zeitwerk`?
3030
----------------------------------------
3131

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).
3333

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.
3535

3636
Rails 7 ends the transition period and does not include `classic` mode.
3737

@@ -80,7 +80,7 @@ config.autoloader = :zeitwerk
8080

8181
In Rails 7 there is only `zeitwerk` mode, you do not need to do anything to enable it.
8282

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.
8484

8585

8686
How to Verify The Application Runs in `zeitwerk` Mode?
@@ -162,6 +162,8 @@ Hold on, I am eager loading the application.
162162
All is good!
163163
```
164164

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+
165167
### Concerns
166168

167169
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.
306308
Check Zeitwerk Compliance in the Test Suite
307309
-------------------------------------------
308310

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.
310312

311313
### Continuous Integration
312314

@@ -349,6 +351,21 @@ RSpec.describe "Zeitwerk compliance" do
349351
end
350352
```
351353

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+
352369
New Features You Can Leverage
353370
-----------------------------
354371

@@ -358,13 +375,12 @@ All known use cases of `require_dependency` have been eliminated with Zeitwerk.
358375

359376
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.
360377

361-
362378
### Qualified Names in Class and Module Definitions Are Now Possible
363379

364380
You can now robustly use constant paths in class and module definitions:
365381

366382
```ruby
367-
# Autoloading in this class' body matches Ruby semantics now.
383+
# Autoloading in this class body matches Ruby semantics now.
368384
class Admin::UsersController < ApplicationController
369385
# ...
370386
end
@@ -398,7 +414,7 @@ end
398414

399415
### Thread-safety Everywhere
400416

401-
In classic mode, constant autoloading is not thread-safe, though Rails has locks in place for example to make web requests thread-safe.
417+
In `classic` mode, constant autoloading is not thread-safe, though Rails has locks in place for example to make web requests thread-safe.
402418

403419
Constant autoloading is thread-safe in `zeitwerk` mode. For example, you can now autoload in multi-threaded scripts executed by the `runner` command.
404420

0 commit comments

Comments
 (0)