Class: RailsAdmin::Config::LazyModel

Inherits:
BasicObject
Defined in:
lib/rails_admin/config/lazy_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(entity, &block) ⇒ LazyModel

Returns a new instance of LazyModel.



8
9
10
11
12
# File 'lib/rails_admin/config/lazy_model.rb', line 8

def initialize(entity, &block)
  @entity = entity
  @deferred_blocks = [*block]
  @initialized = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



65
66
67
# File 'lib/rails_admin/config/lazy_model.rb', line 65

def method_missing(method_name, *args, &block)
  target.send(method_name, *args, &block)
end

Instance Method Details

#add_deferred_block(&block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rails_admin/config/lazy_model.rb', line 14

def add_deferred_block(&block)
  if @initialized
    @model.instance_eval(&block)
  else
    @deferred_blocks << block
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rails_admin/config/lazy_model.rb', line 69

def respond_to_missing?(method_name, include_private = false)
  super || target.respond_to?(method_name, include_private)
end

#targetObject



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
58
59
60
61
62
63
# File 'lib/rails_admin/config/lazy_model.rb', line 22

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 execute 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