-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Load constants lazily on RailsAdmin initialization #3541
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RailsAdmin | ||
| module Config | ||
| module ConstLoadSuppressor | ||
| def suppress_const_load | ||
| original = Object.method(:const_missing) | ||
| Object.define_singleton_method(:const_missing) do |name| | ||
| ConstProxy.new(name.to_s) | ||
| end | ||
| yield | ||
| ensure | ||
| Object.define_singleton_method(:const_missing, original) | ||
| end | ||
|
|
||
| class ConstProxy < BasicObject | ||
| attr_reader :name | ||
|
|
||
| def initialize(name) | ||
| @name = name | ||
| end | ||
|
|
||
| def klass | ||
| @klass ||= | ||
| begin | ||
| unless ::Object.const_defined?(name) | ||
| ::Kernel.raise <<~MESSAGE | ||
| The constant #{name} is not loaded yet upon the execution of the RailsAdmin initializer. | ||
| We don't recommend to do this and may lead to issues, but if you really have to do so you can explicitly require it by adding: | ||
|
|
||
| require '#{name.underscore}' | ||
|
|
||
| on top of config/initializers/rails_admin.rb. | ||
| MESSAGE | ||
| end | ||
| name.constantize | ||
| end | ||
| end | ||
|
|
||
| def method_missing(method_name, *args, &block) | ||
| klass.send(method_name, *args, &block) | ||
| end | ||
|
|
||
| def respond_to_missing?(method_name, include_private = false) | ||
| super || klass.respond_to?(method_name, include_private) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_admin/config/model' | ||
|
|
||
| module RailsAdmin | ||
| module Config | ||
| class LazyModel < BasicObject | ||
| def initialize(entity, &block) | ||
| @entity = entity | ||
| @deferred_blocks = [*block] | ||
| @initialized = false | ||
| end | ||
|
|
||
| def add_deferred_block(&block) | ||
| if @initialized | ||
| @model.instance_eval(&block) | ||
| else | ||
| @deferred_blocks << block | ||
| end | ||
| end | ||
|
|
||
| def target | ||
| @model ||= ::RailsAdmin::Config::Model.new(@entity) | ||
| # When evaluating multiple configuration blocks, the order of | ||
| # execution is important. As one would expect (in my opinion), | ||
| # options defined within a resource should take precedence over | ||
| # more general options defined in an initializer. This way, | ||
| # general settings for a number of resources could be specified | ||
| # in the initializer, while models could override these settings | ||
| # later, if required. | ||
| # | ||
| # CAVEAT: It cannot be guaranteed that blocks defined in an initializer | ||
| # will be loaded (and adde to @deferred_blocks) first. For instance, if | ||
| # the initializer references a model class before defining | ||
| # a RailsAdmin configuration block, the configuration from the | ||
| # resource will get added to @deferred_blocks first: | ||
| # | ||
| # # app/models/some_model.rb | ||
| # class SomeModel | ||
| # rails_admin do | ||
| # : | ||
| # end | ||
| # end | ||
| # | ||
| # # config/initializers/rails_admin.rb | ||
| # model = 'SomeModel'.constantize # blocks from SomeModel get loaded | ||
| # model.config model do # blocks from initializer gets loaded | ||
| # : | ||
| # end | ||
| # | ||
| # Thus, sort all blocks to excute for a resource by Proc.source_path, | ||
| # to guarantee that blocks from 'config/initializers' evaluate before | ||
| # blocks defined within a model class. | ||
| unless @deferred_blocks.empty? | ||
| @deferred_blocks. | ||
| partition { |block| block.source_location.first =~ %r{config/initializers} }. | ||
| flatten. | ||
| each { |block| @model.instance_eval(&block) } | ||
| @deferred_blocks = [] | ||
| end | ||
| @initialized = true | ||
| @model | ||
| end | ||
|
|
||
| def method_missing(method_name, *args, &block) | ||
| target.send(method_name, *args, &block) | ||
| end | ||
|
|
||
| def respond_to_missing?(method_name, include_private = false) | ||
| super || target.respond_to?(method_name, include_private) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.