Class: RailsAdmin::Config::LazyModel
- Inherits:
- BasicObject
- Defined in:
- lib/rails_admin/config/lazy_model.rb
Instance Method Summary collapse
- #add_deferred_block(&block) ⇒ Object
-
#initialize(entity, &block) ⇒ LazyModel
constructor
A new instance of LazyModel.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
- #target ⇒ Object
Constructor Details
#initialize(entity, &block) ⇒ LazyModel
6 7 8 9 10 |
# File 'lib/rails_admin/config/lazy_model.rb', line 6 def initialize(entity, &block) @entity = entity @deferred_blocks = [*block] @existing_blocks = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
59 60 61 |
# File 'lib/rails_admin/config/lazy_model.rb', line 59 def method_missing(method, *args, &block) target.send(method, *args, &block) end |
Instance Method Details
#add_deferred_block(&block) ⇒ Object
12 13 14 |
# File 'lib/rails_admin/config/lazy_model.rb', line 12 def add_deferred_block(&block) @deferred_blocks << block end |
#respond_to?(method, include_private = false) ⇒ Boolean
63 64 65 |
# File 'lib/rails_admin/config/lazy_model.rb', line 63 def respond_to?(method, include_private = false) super || target.respond_to?(method, include_private) end |
#target ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rails_admin/config/lazy_model.rb', line 16 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? @existing_blocks += @deferred_blocks @existing_blocks. partition { |block| block.source_location.first =~ %r{config\/initializers} }. flatten. each { |block| @model.instance_eval(&block) } @deferred_blocks = [] end @model end |