Skip to content

Apply Initializer Prepends on Each Code Reload - #51255

Merged
Hamms merged 1 commit into
stagingfrom
reapply-initializer-prepends
Apr 11, 2023
Merged

Apply Initializer Prepends on Each Code Reload#51255
Hamms merged 1 commit into
stagingfrom
reapply-initializer-prepends

Conversation

@Hamms

@Hamms Hamms commented Apr 11, 2023

Copy link
Copy Markdown
Contributor

Currently, the extra functionality prepended onto these classes by initializer logic is only applied once when the application configuration is initially loaded. This works fine in most environments, but in environments in which we want to be able to reload code on the fly (currently only development) this means that the modifications are lost after reload.

To fix, we hook the prepend call itself into the to_prepare event, which explicitly runs once per code reload in environments which support code reloading, and only once during initial load otherwise.

Links

Testing story

Tested locally to confirm that the functionality added by the initializer is applied to the class after reload, and that repeated reloading does not result in either loss of functionality or repeated reapplying of functionality.

Before this change, after creating a new user account in my local development environment, User.last.sign_ins.count and User.last.user_geos.count both return 0. With this change, both return 1.

Follow-up work

This is required to be able to set cache_classes = false in the test environment

Currently, the extra functionality `prepend`ed onto these classes by initializer logic is only applied once when the application configuration is initially loaded. This works fine in most environments, but in environments in which we want to be able to reload code on the fly (currently only `development`) this means that the modifications are lost after reload.

To fix, we hook the `prepend` call itself into the `to_prepare` event, which explicitly runs once per code reload in environments which support code reloading, and only once during initial load otherwise.

- https://guides.rubyonrails.org/configuring.html#initialization-events
- https://stackoverflow.com/questions/8895103/how-can-i-keep-my-initializer-configuration-from-being-lost-in-development-mode
@Hamms
Hamms marked this pull request as ready for review April 11, 2023 16:34
@Hamms
Hamms requested review from a team and davidsbailey April 11, 2023 16:34

@davidsbailey davidsbailey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice fix! I am already excited for faster tests!

@Hamms
Hamms merged commit 9bf4ed9 into staging Apr 11, 2023
@Hamms
Hamms deleted the reapply-initializer-prepends branch April 11, 2023 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants