Skip to content

Commit 2b3245d

Browse files
committed
Document where custom autoloading inflections should go
1 parent bfa3a5b commit 2b3245d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

guides/source/autoloading_and_reloading_constants.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ By default, Rails uses `String#camelize` to know which constant a given file or
404404

405405
It could be the case that some particular file or directory name does not get inflected as you want. For instance, `html_parser.rb` is expected to define `HtmlParser` by default. What if you prefer the class to be `HTMLParser`? There are a few ways to customize this.
406406

407-
The easiest way is to define acronyms in `config/initializers/inflections.rb`:
407+
The easiest way is to define acronyms:
408408

409409
```ruby
410410
ActiveSupport::Inflector.inflections(:en) do |inflect|
@@ -416,7 +416,6 @@ end
416416
Doing so affects how Active Support inflects globally. That may be fine in some applications, but you can also customize how to camelize individual basenames independently from Active Support by passing a collection of overrides to the default inflectors:
417417

418418
```ruby
419-
# config/initializers/zeitwerk.rb
420419
Rails.autoloaders.each do |autoloader|
421420
autoloader.inflector.inflect(
422421
"html_parser" => "HTMLParser",
@@ -428,7 +427,6 @@ end
428427
That technique still depends on `String#camelize`, though, because that is what the default inflectors use as fallback. If you instead prefer not to depend on Active Support inflections at all and have absolute control over inflections, configure the inflectors to be instances of `Zeitwerk::Inflector`:
429428

430429
```ruby
431-
# config/initializers/zeitwerk.rb
432430
Rails.autoloaders.each do |autoloader|
433431
autoloader.inflector = Zeitwerk::Inflector.new
434432
autoloader.inflector.inflect(
@@ -442,7 +440,11 @@ There is no global configuration that can affect said instances; they are determ
442440

443441
You can even define a custom inflector for full flexibility. Please check the [Zeitwerk documentation](https://github.com/fxn/zeitwerk#custom-inflector) for further details.
444442

445-
NOTE: The inflector in initializers won't take effect on the `once` autoloader. Consider putting it to `config/application.rb` if you need the inflection for the `once` autoloader.
443+
### Where Should Inflection Customization Go?
444+
445+
If an application does not use the `once` autoloader, the snippets above can go in `config/initializers`. For example, `config/initializers/inflections.rb` for the Active Support use case, or `config/initializers/zeitwerk.rb` for the other ones.
446+
447+
Applications using the `once` autoloader have to move or load this configuration from the body of the application class in `config/application.rb`, because the `once` autoloader uses the inflector early in the boot process.
446448

447449
Autoloading and Engines
448450
-----------------------

0 commit comments

Comments
 (0)