Skip to content

Commit dcbd6b0

Browse files
committed
Revise the autoloading guide for middleware and friends
1 parent bac70c3 commit dcbd6b0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

guides/source/autoloading_and_reloading_constants.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,21 @@ That adds a module object to the ancestor chain of `ActiveRecord::Base`. Changes
368368

369369
Corollary: Those classes or modules **cannot be reloadable**.
370370

371-
The easiest way to refer to those classes or modules during boot is to have them defined in a directory which does not belong to the autoload paths. For instance, `lib` is an idiomatic choice. It does not belong to the autoload paths by default, but it does belong to `$LOAD_PATH`. Just perform a regular `require` to load it.
371+
An idiomatic way to organize these files is to put them in the `lib` directory and load them with `require` where needed. For example, if the application has custom middleware in `lib/middleware`, issue a regular `require` call before configuring it:
372+
373+
```ruby
374+
require "middleware/my_middleware"
375+
config.middleware.use MyMiddleware
376+
```
377+
378+
Additionally, if `lib` is in the autoload paths, configure the autoloader to ignore that subdirectory:
379+
380+
```ruby
381+
# config/application.rb
382+
config.autoload_lib(ignore: %w(assets tasks ... middleware))
383+
```
384+
385+
since you are loading those files yourself.
372386

373387
As noted above, another option is to have the directory that defines them in the autoload once paths and autoload. Please check the [section about config.autoload_once_paths](#config-autoload-once-paths) for details.
374388

0 commit comments

Comments
 (0)