Skip to content

Commit 11abd3e

Browse files
authored
Merge pull request #14 from willnet/evolve-readme
Update README.md to enhance lazy loading explanation and configuration details
2 parents bf0804f + 81d2d59 commit 11abd3e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# A::NtiMannerKickCourse
22

3+
## Introduction
34
Rails speeds up the startup process by lazily loading classes for components like ActiveRecord::Base and ActionController::Base. Additionally, the behavior of lazy loading is essential for applying configuration settings to these components. Without lazy loading, some configurations might not be applied as expected.
45

6+
### Why Lazy Loading Matters
7+
Without lazy loading, certain settings might not be applied in time.
8+
59
For example, consider the following scenario:
610

11+
### Example: Upgrading from Rails 7.0 to 7.1
712
When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates a file named `config/initializers/new_framework_defaults_7_1.rb`. This file helps incrementally enable new default settings for Rails 7.1. Initially, all settings in this file are commented out. Uncommenting the following setting will enable it:
813

914
```ruby
@@ -12,8 +17,9 @@ When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates
1217
# as equal to an equivalent `Hash` by default.
1318
#++
1419
Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false
15-
````
20+
```
1621

22+
### Configuration Details and Pitfalls
1723
As the comment suggests, this setting ensures that ActionController::Parameters instances are no longer treated as equivalent to Hash. However, if ActionController::Base is eager-loaded, this configuration will not behave as expected. Why does this happen?
1824

1925
Configuration settings like `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality` are not automatically effective. These settings are applied to components like ActionController only during the Rails initialization process.
@@ -32,7 +38,7 @@ This assignment is wrapped in an `ActiveSupport.on_load(:action_controller, run_
3238
ActiveSupport.on_load(:action_controller, run_once: true) do
3339
# Configuration assignment logic
3440
end
35-
````
41+
```
3642

3743
Now, consider a gem "add_some_function_to_controller" with the following code:
3844

@@ -52,8 +58,7 @@ Since all gems listed in the Gemfile are required in config/application.rb, the
5258

5359
As a result, the configuration logic for ActionController is executed before the initializer file `config/initializers/new_framework_defaults_7_1.rb`. This means that the setting `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false` is not applied in time.
5460

55-
Fixing the Issue
56-
61+
### Fixing the Issue
5762
To solve this, modify the gem code as follows:
5863

5964
```ruby
@@ -76,6 +81,7 @@ This resolves the issue, as the setting in config/initializers/new_framework_def
7681

7782
While this specific case is likely a common issue, other problems may also arise from changes to the initialization order. To ensure reliable behavior, it is critical to keep all component loading deferred during Rails initialization. However, manually checking for potential issues with lazy loading is impractical.
7883

84+
### Using a-nti_manner_kick_course
7985
Using a-nti_manner_kick_course can help detect libraries or application code (like add_some_function_to_controller) that interfere with lazy loading, allowing you to maintain proper initialization behavior.
8086

8187
## Installation
@@ -110,7 +116,7 @@ $ ANTI_MANNER=1 rails runner 1
110116

111117
If any code fails to lazy loading, the process will exit with status code 1. This command suggests which lines are eager loading Rails code. It is a good idea to regularly check in CI if lazy loading is working correctly.
112118

113-
You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
119+
You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
114120

115121
For more details about ActiveSupport.on_load, check [the official Rails documentation](https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html).
116122

0 commit comments

Comments
 (0)