Skip to content

Commit f14b19a

Browse files
committed
Edits to the classic migration guide
1 parent bc590ff commit f14b19a

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

guides/source/classic_to_zeitwerk_howto.md

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ After reading this guide, you will know:
1414
* How to verify your project loads OK in the command line
1515
* How to verify your project loads OK in the test suite
1616
* How to address possible edge cases
17+
* New features in Zeitwerk you can leverage
1718

1819
--------------------------------------------------------------------------------
1920

@@ -288,7 +289,7 @@ require "test_helper"
288289

289290
class ZeitwerkComplianceTest < ActiveSupport::TestCase
290291
test "eager loads all files without errors" do
291-
Zeitwerk::Loader.eager_load_all
292+
Rails.application.eager_load!
292293
rescue => e
293294
flunk(e.message)
294295
else
@@ -304,22 +305,38 @@ require "rails_helper"
304305

305306
RSpec.describe "Zeitwerk compliance" do
306307
it "eager loads all files without errors" do
307-
expect{ Zeitwerk::Loader.eager_load_all }.not_to raise_error
308+
expect{ Rails.application.eager_load! }.not_to raise_error
308309
end
309310
end
310311
```
311312

313+
### Globs in `config.autoload_paths`
312314

313-
Delete `require_dependency` calls
314-
---------------------------------
315+
Beware of configurations like
316+
317+
```ruby
318+
config.autoload_paths += Dir["#{config.root}/lib/**/"]
319+
```
320+
321+
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).
322+
323+
To fix this, just remove the wildcards:
324+
325+
```ruby
326+
config.autoload_paths << "#{config.root}/lib"
327+
```
328+
329+
New Features You Can Leverage
330+
-----------------------------
331+
332+
### Delete `require_dependency` calls
315333

316334
All known use cases of `require_dependency` have been eliminated with Zeitwerk. You should grep the project and delete them.
317335

318336
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.
319337

320338

321-
Qualified Names in Class and Module Definitions Are Now Possible
322-
----------------------------------------------------------------
339+
### Qualified Names in Class and Module Definitions Are Now Possible
323340

324341
You can now robustly use constant paths in class and module definitions:
325342

@@ -356,35 +373,13 @@ module Foo
356373
end
357374
```
358375

359-
360-
Thread-safety
361-
-------------
376+
### Thread-safety Everywhere
362377

363378
In classic mode, constant autoloading is not thread-safe, though Rails has locks in place for example to make web requests thread-safe.
364379

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

367-
368-
Globs in `config.autoload_paths`
369-
--------------------------------
370-
371-
Beware of configurations like
372-
373-
```ruby
374-
config.autoload_paths += Dir["#{config.root}/lib/**/"]
375-
```
376-
377-
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).
378-
379-
To fix this, just remove the wildcards:
380-
381-
```ruby
382-
config.autoload_paths << "#{config.root}/lib"
383-
```
384-
385-
386-
Eager loading and autoloading are consistent
387-
--------------------------------------------
382+
### Eager Loading and Autoloading are Consistent
388383

389384
In `classic` mode, if `app/models/foo.rb` defines `Bar`, you won't be able to autoload that file, but eager loading will work because it loads files recursively blindly. This can be a source of errors if you test things first eager loading, execution may fail later autoloading.
390385

0 commit comments

Comments
 (0)