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

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

NAMED_INSTANCE_VARIABLES =
[
  :@parent, :@root, :@section, :@children_fields_registered,
  :@associated_model_config, :@group
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

#inspect

Methods included from Groupable

#group

Methods included from Hideable

#hidden?, #hide, included, #show

Methods included from Configurable

#has_option?, #register_deprecated_instance_option, #register_instance_option

Methods included from Proxyable

#bindings, #bindings=, #with

Constructor Details

#initialize(parent, name, properties) ⇒ Base

Returns a new instance of Base.


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

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


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

def abstract_model
  @abstract_model
end

#definedObject

Returns the value of attribute defined


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

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#orderObject

Returns the value of attribute order


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

def order
  @order
end

#parentObject (readonly)

Returns the value of attribute parent


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

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties


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

def properties
  @properties
end

#rootObject (readonly)

Returns the value of attribute root


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

def root
  @root
end

#sectionObject

Returns the value of attribute section


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

def section
  @section
end

Instance Method Details

#association?Boolean

Is this an association

Returns:

  • (Boolean)

235
236
237
# File 'lib/rails_admin/config/fields/base.rb', line 235

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

#editable?Boolean

Returns:

  • (Boolean)

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

def editable?
  !(@properties && @properties.read_only?)
end

#errorsObject

Reader for validation errors of the bound object


240
241
242
243
244
# File 'lib/rails_admin/config/fields/base.rb', line 240

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

#form_default_valueObject


327
328
329
# File 'lib/rails_admin/config/fields/base.rb', line 327

def form_default_value
  (default_value if bindings[:object].new_record? && value.nil?)
end

#form_valueObject


331
332
333
# File 'lib/rails_admin/config/fields/base.rb', line 331

def form_value
  form_default_value.nil? ? formatted_value : form_default_value
end

#generic_field_helpObject


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

def generic_field_help
  model = abstract_model.model_name.underscore
  model_lookup = "admin.help.#{model}.#{name}".to_sym
  translated = I18n.translate(model_lookup, help: generic_help, default: [generic_help])
  (translated.is_a?(Hash) ? translated.to_a.first[1] : translated).html_safe
end

#generic_helpObject


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

def generic_help
  (required? ? I18n.translate('admin.form.required') : I18n.translate('admin.form.optional')) + '. '
end

#inverse_ofObject


319
320
321
# File 'lib/rails_admin/config/fields/base.rb', line 319

def inverse_of
  nil
end

#method_nameObject


323
324
325
# File 'lib/rails_admin/config/fields/base.rb', line 323

def method_name
  name
end

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

Inverse accessor whether this field is required.

See Also:

  • :required?

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

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

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

def optional=(state)
  optional(state)
end

#optional?Boolean

Reader whether field is optional.

Returns:

  • (Boolean)

See Also:

  • :required?

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

def optional?
  !required?
end

#parse_input(_params) ⇒ Object


315
316
317
# File 'lib/rails_admin/config/fields/base.rb', line 315

def parse_input(_params)
  # overriden
end

#parse_value(value) ⇒ Object


311
312
313
# File 'lib/rails_admin/config/fields/base.rb', line 311

def parse_value(value)
  value
end

#typeObject

Reader for field's type


272
273
274
# File 'lib/rails_admin/config/fields/base.rb', line 272

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

#type_css_classObject


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

def type_css_class
  "#{type}_type"
end

#valueObject

Reader for field's value


277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rails_admin/config/fields/base.rb', line 277

def value
  bindings[:object].safe_send(name)
rescue NoMethodError => e
  raise e.exception <<-EOM.gsub(/^\s{10}/, '')
  #{e.message}
  If you want to use a RailsAdmin virtual field(= a field without corresponding instance method),
  you should declare 'formatted_value' in the field definition.
    field :#{name} do
      formatted_value{ bindings[:object].call_some_method }
    end
  EOM
end

#virtual?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/rails_admin/config/fields/base.rb', line 46

def virtual?
  properties.blank?
end