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

Inherits:
Base
  • Object
show all
Includes:
Hideable
Defined in:
lib/rails_admin/config/fields/base.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#abstract_model, #bindings, #parent, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hideable

#hidden?, #hide, included, #show

Methods inherited from Base

#has_option?, register_class_option, #register_deprecated_instance_option, register_deprecated_instance_option, register_instance_option, #register_instance_option, #with

Constructor Details

#initialize(parent, name, properties) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_admin/config/fields/base.rb', line 23

def initialize(parent, name, properties)
  super(parent)

  @defined = false
  @name = name
  @order = 0
  @properties = properties

  # If parent is able to group fields the field should be aware of it
  if parent.kind_of?(RailsAdmin::Config::HasGroups)
    extend RailsAdmin::Config::Fields::Groupable
  end
end

Instance Attribute Details

#definedObject

Returns the value of attribute defined.



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

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/rails_admin/config/fields/base.rb', line 13

def name
  @name
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#propertiesObject (readonly)

Returns the value of attribute properties.



13
14
15
# File 'lib/rails_admin/config/fields/base.rb', line 13

def properties
  @properties
end

Class Method Details

.inherited(klass) ⇒ Object



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

def self.inherited(klass)
    klass.instance_variable_set("@css_class", klass.name.to_s.demodulize.camelcase(:lower))
    klass.instance_variable_set("@view_helper", :text_field)
end

Instance Method Details

#association?Boolean

Is this an association

Returns:

  • (Boolean)


203
204
205
# File 'lib/rails_admin/config/fields/base.rb', line 203

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

#column_css_class(*args, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rails_admin/config/fields/base.rb', line 41

def column_css_class(*args, &block)
  if !args[0].nil? || block
    @css_class = args[0].nil? ? block : args[0]
  else
    css_class
  end
end

#dom_idObject

Reader for field’s id



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

def dom_id
  @dom_id ||= [
    bindings[:form].object_name,
    bindings[:form].options[:index],
    method_name
  ].reject(&:blank?).join('_')
end

#dom_nameObject

Reader for field’s name



254
255
256
# File 'lib/rails_admin/config/fields/base.rb', line 254

def dom_name
  @dom_name ||= "#{bindings[:form].object_name}#{(index = bindings[:form].options[:index]) && "[#{index}]"}[#{method_name}]"
end

#errorsObject

Reader for validation errors of the bound object



208
209
210
# File 'lib/rails_admin/config/fields/base.rb', line 208

def errors
  bindings[:object].errors[name]
end

#has_errors?Boolean

Reader whether the bound object has validation errors

Returns:

  • (Boolean)


213
214
215
216
# File 'lib/rails_admin/config/fields/base.rb', line 213

def has_errors?
  # TODO DEPRECATE, USELESS
  errors.present?
end

#method_nameObject



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

def method_name
  name
end

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

Inverse accessor whether this field is required.

See Also:

  • register_instance_option(:required?)


228
229
230
231
232
233
234
# File 'lib/rails_admin/config/fields/base.rb', line 228

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


239
240
241
# File 'lib/rails_admin/config/fields/base.rb', line 239

def optional=(state)
  optional(state)
end

#optional?Boolean

Reader whether field is optional.

Returns:

  • (Boolean)

See Also:

  • register_instance_option(:required?)


221
222
223
# File 'lib/rails_admin/config/fields/base.rb', line 221

def optional?
  not required
end

#typeObject

Reader for field’s type



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

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

#valueObject

Reader for field’s value



249
250
251
# File 'lib/rails_admin/config/fields/base.rb', line 249

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

#virtual?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rails_admin/config/fields/base.rb', line 49

def virtual?
  properties.blank?
end