Class: RailsAdmin::Config::Fields::Base

Inherits:
Object
  • Object
show all
Includes:
Configurable, Groupable, Hideable, Proxyable
Defined in:
lib/rails_admin/config/fields/base.rb

Instance Attribute Summary collapse

Attributes included from Proxyable

#bindings

Instance Method Summary collapse

Methods included from Groupable

#group

Methods included from Hideable

#hidden?, #hide, included, #show

Methods included from Configurable

#has_option?, included, #register_deprecated_instance_option, #register_instance_option

Methods included from Proxyable

#with

Constructor Details

#initialize(parent, name, properties) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_admin/config/fields/base.rb', line 19

def initialize(parent, name, properties)
  @parent = parent
  @root = parent.root

  @abstract_model = parent.abstract_model
  @defined = false
  @name = name.to_sym
  @order = 0
  @properties = properties
  @section = parent
end

Instance Attribute Details

#abstract_modelObject (readonly)

Returns the value of attribute abstract_model.



15
16
17
# File 'lib/rails_admin/config/fields/base.rb', line 15

def abstract_model
  @abstract_model
end

#definedObject

Returns the value of attribute defined.



16
17
18
# File 'lib/rails_admin/config/fields/base.rb', line 16

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/rails_admin/config/fields/base.rb', line 15

def name
  @name
end

#orderObject

Returns the value of attribute order.



16
17
18
# File 'lib/rails_admin/config/fields/base.rb', line 16

def order
  @order
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties.



15
16
17
# File 'lib/rails_admin/config/fields/base.rb', line 15

def properties
  @properties
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#sectionObject

Returns the value of attribute section.



16
17
18
# File 'lib/rails_admin/config/fields/base.rb', line 16

def section
  @section
end

Instance Method Details

#association?Boolean

Is this an association

Returns:

  • (Boolean)


245
246
247
# File 'lib/rails_admin/config/fields/base.rb', line 245

def association?
  kind_of?(RailsAdmin::Config::Fields::Association)
end

#editable?Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rails_admin/config/fields/base.rb', line 216

def editable?
  return false if @properties && @properties[:read_only]
  role = bindings[:view].controller.send(:_attr_accessible_role)
  active_model_attr_accessible = !bindings[:object].class.active_authorizer[role].deny?(self.method_name)

  return true if active_model_attr_accessible
  if RailsAdmin::Config.yell_for_non_accessible_fields
    accessible = "attr_accessible :#{self.method_name}#{role == :default ? '' : ", :as => :#{role}"}"

    Rails.logger.debug <<-MESSAGE.strip_heredoc


    [RailsAdmin] Please add '#{accessible}' in your '#{bindings[:object].class}' model definition if you want to make it editable.
    You can also explicitely mark this field as read-only:

    config.model #{bindings[:object].class} do
      field :#{self.name} do
        read_only true
      end
    end

    Add 'config.yell_for_non_accessible_fields = false' in your 'rails_admin.rb' initializer if you do not want to see these warnings

    MESSAGE
  end
  false
end

#errorsObject

Reader for validation errors of the bound object



250
251
252
253
254
# File 'lib/rails_admin/config/fields/base.rb', line 250

def errors
  ([name] + children_fields).uniq.map do |column_name|
    bindings[:object].errors[column_name]
  end.uniq.flatten
end

#html_default_valueObject



313
314
315
# File 'lib/rails_admin/config/fields/base.rb', line 313

def html_default_value
  bindings[:object].new_record? && self.value.nil? && !self.default_value.nil? ? self.default_value : nil
end

#inspectObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rails_admin/config/fields/base.rb', line 318

def inspect
  "#<#{self.class.name}[#{name}] #{
    instance_variables.map do |v|
      value = instance_variable_get(v)
      if [:@parent, :@root, :@section, :@children_fields_registered,
          :@associated_model_config, :@group, :@bindings].include? v
        if value.respond_to? :name
          "#{v}=#{value.name.inspect}"
        else
          "#{v}=#{value.class.name}"
        end
      else
        "#{v}=#{value.inspect}"
      end
    end.join(", ")
  }>"
end

#inverse_ofObject



305
306
307
# File 'lib/rails_admin/config/fields/base.rb', line 305

def inverse_of
  nil
end

#method_nameObject



309
310
311
# File 'lib/rails_admin/config/fields/base.rb', line 309

def method_name
  name
end

#optional(state = nil, &block) ⇒ Object

Inverse accessor whether this field is required.

See Also:

  • :required?


266
267
268
269
270
271
272
# File 'lib/rails_admin/config/fields/base.rb', line 266

def optional(state = nil, &block)
  if !state.nil? || block
    required state.nil? ? proc { false == (instance_eval &block) } : false == state
  else
    optional?
  end
end

#optional=(state) ⇒ Object

Writer to make field optional.

See Also:

  • optional


277
278
279
# File 'lib/rails_admin/config/fields/base.rb', line 277

def optional=(state)
  optional(state)
end

#optional?Boolean

Reader whether field is optional.

Returns:

  • (Boolean)

See Also:

  • :required?


259
260
261
# File 'lib/rails_admin/config/fields/base.rb', line 259

def optional?
  not required?
end

#parse_input(params) ⇒ Object



301
302
303
# File 'lib/rails_admin/config/fields/base.rb', line 301

def parse_input(params)
  # overriden
end

#typeObject

Reader for field’s type



282
283
284
# File 'lib/rails_admin/config/fields/base.rb', line 282

def type
  @type ||= self.class.name.to_s.demodulize.underscore.to_sym
end

#type_css_classObject



35
36
37
# File 'lib/rails_admin/config/fields/base.rb', line 35

def type_css_class
  "#{type}_type"
end

#valueObject

Reader for field’s value



287
288
289
# File 'lib/rails_admin/config/fields/base.rb', line 287

def value
  bindings[:object].safe_send(name)
end

#virtual?Boolean

Returns:

  • (Boolean)


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

def virtual?
  properties.blank?
end