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
Those examples were obsolete, because nowadays using a reloadable
constant in initializers is not just a bad idea, it is indeed not
possible.
The patch updates the examples accordingly.
a reloaded `ApiGateway` would have a `nil` endpoint, because the code above does not run again.
251
-
252
-
You can still set things up during boot, but you need to wrap them in a `to_prepare` block, which runs on boot, and after each reload:
250
+
Initializers cannot refer to reloadable constants, you need to wrap that in a `to_prepare` block, which runs on boot, and after each reload:
253
251
254
252
```ruby
255
253
# config/initializers/api_gateway_setup.rb
@@ -277,15 +275,15 @@ end
277
275
278
276
### Use Case 2: During Boot, Load Code that Remains Cached
279
277
280
-
Some configurations take a class or module object, and they store it in a place that is not reloaded.
278
+
Some configurations take a class or module object, and they store it in a place that is not reloaded. It is important that these are not reloadable, because edits would not be reflected in those cached stale objects.
281
279
282
280
One example is middleware:
283
281
284
282
```ruby
285
283
config.middleware.use MyApp::Middleware::Foo
286
284
```
287
285
288
-
When you reload, the middleware stack is not affected, so, whatever object was stored in`MyApp::Middleware::Foo`at boot time remains there stale.
286
+
When you reload, the middleware stack is not affected, so it would be confusing that`MyApp::Middleware::Foo`is reloadable. Changes in its implementation would have no effect.
289
287
290
288
Another example is Active Job serializers:
291
289
@@ -294,7 +292,7 @@ Another example is Active Job serializers:
Whatever `MoneySerializer` evaluates to during initialization gets pushed to the custom serializers. If that was reloadable, the initial object would be still within Active Job, not reflecting your changes.
295
+
Whatever `MoneySerializer` evaluates to during initialization gets pushed to the custom serializers, and that object stays there on reloads.
298
296
299
297
Yet another example are railties or engines decorating framework classes by including modules. For instance, [`turbo-rails`](https://github.com/hotwired/turbo-rails) decorates `ActiveRecord::Base` this way:
300
298
@@ -321,12 +319,10 @@ Let's suppose an engine works with the reloadable application class that models
321
319
```ruby
322
320
# config/initializers/my_engine.rb
323
321
MyEngine.configure do |config|
324
-
config.user_model =User#DO NOT DO THIS
322
+
config.user_model =User#NameError
325
323
end
326
324
```
327
325
328
-
On reload, `config.user_model` would be pointing to a stale object, because the reloaded `User` class would not be reset in the engine configuration. Therefore, edits to `User` would be missed by the engine.
329
-
330
326
In order to play well with reloadable application code, the engine instead needs applications to configure the _name_ of that class:
0 commit comments