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/configuring.md
+129Lines changed: 129 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3453,6 +3453,135 @@ NOTE: There is no guarantee that your initializers will run after all the gem
3453
3453
initializers, so any initialization code that depends on a given gem having been
3454
3454
initialized should go into a `config.after_initialize` block.
3455
3455
3456
+
Load Hooks
3457
+
----------------------------
3458
+
3459
+
Rails code can often be referenced on load of an application. Rails is responsible for the load order of these frameworks, so when you load frameworks, such as `ActiveRecord::Base`, prematurely you are violating an implicit contract your application has with Rails. Moreover, by loading code such as `ActiveRecord::Base` on boot of your application you are loading entire frameworks which may slow down your boot time and could cause conflicts with load order and boot of your application.
3460
+
3461
+
Load and configuration hooks are the API that allow you to hook into this initialization process without violating the load contract with Rails. This will also mitigate boot performance degradation and avoid conflicts.
3462
+
3463
+
### Avoid Loading Rails Frameworks
3464
+
3465
+
Since Ruby is a dynamic language, some code will cause different Rails frameworks to load. Take this snippet for instance:
3466
+
3467
+
```ruby
3468
+
ActiveRecord::Base.include(MyActiveRecordHelper)
3469
+
```
3470
+
3471
+
This snippet means that when this file is loaded, it will encounter `ActiveRecord::Base`. This encounter causes Ruby to look for the definition of that constant and will require it. This causes the entire Active Record framework to be loaded on boot.
3472
+
3473
+
`ActiveSupport.on_load` is a mechanism that can be used to defer the loading of code until it is actually needed. The snippet above can be changed to:
3474
+
3475
+
```ruby
3476
+
ActiveSupport.on_load(:active_record) do
3477
+
include MyActiveRecordHelper
3478
+
end
3479
+
```
3480
+
3481
+
This new snippet will only include `MyActiveRecordHelper` when `ActiveRecord::Base` is loaded.
3482
+
3483
+
### When are Hooks called?
3484
+
3485
+
In the Rails framework these hooks are called when a specific library is loaded. For example, when `ActionController::Base` is loaded, the `:action_controller_base` hook is called. This means that all `ActiveSupport.on_load` calls with `:action_controller_base` hooks will be called in the context of `ActionController::Base` (that means `self` will be an `ActionController::Base`).
3486
+
3487
+
### Modifying Code to Use Load Hooks
3488
+
3489
+
Modifying code is generally straightforward. If you have a line of code that refers to a Rails framework such as `ActiveRecord::Base` you can wrap that code in a load hook.
Copy file name to clipboardExpand all lines: guides/source/engines.md
-154Lines changed: 0 additions & 154 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1369,157 +1369,3 @@ module MyEngine
1369
1369
end
1370
1370
end
1371
1371
```
1372
-
1373
-
Load and Configuration Hooks
1374
-
----------------------------
1375
-
1376
-
Rails code can often be referenced on load of an application. Rails is responsible for the load order of these frameworks, so when you load frameworks, such as `ActiveRecord::Base`, prematurely you are violating an implicit contract your application has with Rails. Moreover, by loading code such as `ActiveRecord::Base` on boot of your application you are loading entire frameworks which may slow down your boot time and could cause conflicts with load order and boot of your application.
1377
-
1378
-
Load and configuration hooks are the API that allow you to hook into this initialization process without violating the load contract with Rails. This will also mitigate boot performance degradation and avoid conflicts.
1379
-
1380
-
### Avoid Loading Rails Frameworks
1381
-
1382
-
Since Ruby is a dynamic language, some code will cause different Rails frameworks to load. Take this snippet for instance:
1383
-
1384
-
```ruby
1385
-
ActiveRecord::Base.include(MyActiveRecordHelper)
1386
-
```
1387
-
1388
-
This snippet means that when this file is loaded, it will encounter `ActiveRecord::Base`. This encounter causes Ruby to look for the definition of that constant and will require it. This causes the entire Active Record framework to be loaded on boot.
1389
-
1390
-
`ActiveSupport.on_load` is a mechanism that can be used to defer the loading of code until it is actually needed. The snippet above can be changed to:
1391
-
1392
-
```ruby
1393
-
ActiveSupport.on_load(:active_record) do
1394
-
includeMyActiveRecordHelper
1395
-
end
1396
-
```
1397
-
1398
-
This new snippet will only include `MyActiveRecordHelper` when `ActiveRecord::Base` is loaded.
1399
-
1400
-
### When are Hooks called?
1401
-
1402
-
In the Rails framework these hooks are called when a specific library is loaded. For example, when `ActionController::Base` is loaded, the `:action_controller_base` hook is called. This means that all `ActiveSupport.on_load` calls with `:action_controller_base` hooks will be called in the context of `ActionController::Base` (that means `self` will be an `ActionController::Base`).
1403
-
1404
-
### Modifying Code to Use Load Hooks
1405
-
1406
-
Modifying code is generally straightforward. If you have a line of code that refers to a Rails framework such as `ActiveRecord::Base` you can wrap that code in a load hook.
0 commit comments