Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load constants lazily on RailsAdmin initialization #3541

Merged
merged 3 commits into from
Jul 18, 2022

Conversation

mshibuya
Copy link
Member

This is a counter-proposal for #3492, and aims to fix issues #3490, #3501, #3533, and #3534.

My apologies for those issues. This is because of the complexity in RailsAdmin (and Rails) initialization process, which is hard to get it right. Let me explain the story...

RailsAdmin initializer (config/initializers/rails_admin.rb) is loaded during the Rails app's startup process. But as for the actual configuration (= execution of the DSL itself, the code passed in the RailsAdmin.config do |config| ... end block), there're many constraints that all need to be fulfilled for a successful initialization.

So the attempt to address this was made in e4ae669. The idea was basically to delay the execution of the configuration block, after model classes are ready to be autoloaded. But it brought different problems:

Thus, it's like 2 contradicting forces pulling the initialization timing both forward ant backward. That's why I have been struggling in solving this issue without success...

We need a new way to overcome this problem, and here I have come up with one. That is to intercept const_missing to eliminate the necessity for loading actual classes, while delaying the model initialization by utilizing LazyModel, which is the proven method from pre RailsAdmin 3.x era.
At the first sight overriding Object.const_missing seemed to have side effects, but from what I tested it looks working well. So I want you guys to try this in your setup, and give feedback on whether this is good idea or not.

Thank you for reading this through, and hoping to hearing back!

@codealchemy @jdufresne @q3aiml (or anyone who is interested in this)

@coveralls
Copy link

Coverage Status

Coverage remained the same at 89.498% when pulling 27002eb on load-constants-lazily into 0ebbd85 on master.

Copy link
Contributor

@codealchemy codealchemy left a comment

Choose a reason for hiding this comment

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

I can confirm that this change addresses the issues I was running into in #3534

Some thoughts re: allowing model constants to be used in the initializer.

@mshibuya mshibuya merged commit f84ca1c into master Jul 18, 2022
@mshibuya
Copy link
Member Author

Merged. Thank you for reviewing!

@q3aiml
Copy link
Contributor

q3aiml commented Jul 18, 2022

This is working well in my testing and resolves #3490. Thank you for figuring this out!

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.

4 participants