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/autoloading_and_reloading_constants.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -404,7 +404,7 @@ By default, Rails uses `String#camelize` to know which constant a given file or
404
404
405
405
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.
406
406
407
-
The easiest way is to define acronyms in `config/initializers/inflections.rb`:
407
+
The easiest way is to define acronyms:
408
408
409
409
```ruby
410
410
ActiveSupport::Inflector.inflections(:en) do |inflect|
@@ -416,7 +416,6 @@ end
416
416
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:
417
417
418
418
```ruby
419
-
# config/initializers/zeitwerk.rb
420
419
Rails.autoloaders.each do |autoloader|
421
420
autoloader.inflector.inflect(
422
421
"html_parser" => "HTMLParser",
@@ -428,7 +427,6 @@ end
428
427
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`:
429
428
430
429
```ruby
431
-
# config/initializers/zeitwerk.rb
432
430
Rails.autoloaders.each do |autoloader|
433
431
autoloader.inflector =Zeitwerk::Inflector.new
434
432
autoloader.inflector.inflect(
@@ -442,7 +440,11 @@ There is no global configuration that can affect said instances; they are determ
442
440
443
441
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.
444
442
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.
0 commit comments