Class: RailsAdmin::Config::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/config/base.rb

Overview

A base class for all configurables.

Each configurable has a parent object. This parent object must provide the configurable with abstract_model and bindings.

Bindings is a hash of variables bound by the querying context. For example the list view’s template will bind an object to key :object for each row it outputs. This object is the actual row object which is then used as the receiver of queries for property values.

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
# File 'lib/rails_admin/config/base.rb', line 20

def initialize(parent)
  @abstract_model = parent.abstract_model
  @bindings = {}
  @parent = parent
  @root = parent.root
end

Instance Attribute Details

#abstract_modelObject (readonly)

Returns the value of attribute abstract_model.



18
19
20
# File 'lib/rails_admin/config/base.rb', line 18

def abstract_model
  @abstract_model
end

#bindingsObject (readonly)

Returns the value of attribute bindings.



18
19
20
# File 'lib/rails_admin/config/base.rb', line 18

def bindings
  @bindings
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/rails_admin/config/base.rb', line 18

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



18
19
20
# File 'lib/rails_admin/config/base.rb', line 18

def root
  @root
end

Class Method Details

.register_class_option(option_name, &default) ⇒ Object

Register a class option. Class option is a configuration option that stores it’s value within a class object’s instance variable and is accessed by a class method. Both go by the name of the option.



105
106
107
108
# File 'lib/rails_admin/config/base.rb', line 105

def self.register_class_option(option_name, &default)
  scope = class << self; self; end;
  self.register_instance_option(option_name, scope, &default)
end

.register_deprecated_instance_option(option_name, replacement_option_name, scope = self) ⇒ Object



95
96
97
98
99
100
# File 'lib/rails_admin/config/base.rb', line 95

def self.register_deprecated_instance_option(option_name, replacement_option_name, scope = self)
  scope.send(:define_method, option_name) do |*args, &block|
    ActiveSupport::Deprecation.warn("The #{option_name} configuration option is deprecated, please use #{replacement_option_name}.")
    send(replacement_option_name, *args, &block)
  end
end

.register_instance_option(option_name, scope = self, &default) ⇒ Object

Register an instance option. Instance option is a configuration option that stores its value within an instance variable and is accessed by an instance method. Both go by the name of the option.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rails_admin/config/base.rb', line 50

def self.register_instance_option(option_name, scope = self, &default)
  unless options = scope.instance_variable_get("@config_options")
    options = scope.instance_variable_set("@config_options", {})
  end

  option_name = option_name.to_s

  options[option_name] = nil

  # If it's a boolean create an alias for it and remove question mark
  if "?" == option_name[-1, 1]
    scope.send(:define_method, "#{option_name.chop!}?") do
      send(option_name)
    end
  end

  # Define getter/setter by the option name
  scope.send(:define_method, option_name) do |*args, &block|
    if !args[0].nil? || block
      # Invocation with args --> This is the declaration of the option, i.e. setter
      instance_variable_set("@#{option_name}_registered", args[0].nil? ? block : args[0])
    else
      # Invocation without args nor block --> It's the use of the option, i.e. getter
      value = instance_variable_get("@#{option_name}_registered")
      case value
        when Proc
          # Track recursive invocation with an instance variable. This prevents run-away recursion
          # and allows configurations such as
          # label { "#{label}".upcase }
          # This will use the default definition when called recursively.
          if instance_variable_get("@#{option_name}_recurring")
            value = instance_eval &default
          else
            instance_variable_set("@#{option_name}_recurring", true)
            value = instance_eval &value
            instance_variable_set("@#{option_name}_recurring", false)
          end
        when nil
          value = instance_eval &default
      end
      value
    end
  end
end

Instance Method Details

#has_option?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/rails_admin/config/base.rb', line 27

def has_option?(name)
  options = self.class.instance_variable_get("@config_options")
  options && options.has_key?(name)
end

#register_deprecated_instance_option(option_name, replacement_option_name) ⇒ Object



38
39
40
41
# File 'lib/rails_admin/config/base.rb', line 38

def register_deprecated_instance_option(option_name, replacement_option_name)
  scope = class << self; self; end;
  self.class.register_deprecated_instance_option(option_name, replacement_option_name, scope)
end

#register_instance_option(option_name, &default) ⇒ Object

Register an instance option for this object only



33
34
35
36
# File 'lib/rails_admin/config/base.rb', line 33

def register_instance_option(option_name, &default)
  scope = class << self; self; end;
  self.class.register_instance_option(option_name, scope, &default)
end

#with(bindings = {}) ⇒ Object



43
44
45
# File 'lib/rails_admin/config/base.rb', line 43

def with(bindings = {})
  RailsAdmin::Config::Proxy.new(self, bindings)
end