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 =
%i[
  @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?, included, #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.



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

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.



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

def abstract_model
  @abstract_model
end

#definedObject

Returns the value of attribute defined.



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

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#orderObject

Returns the value of attribute order.



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

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.



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

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.



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

def section
  @section
end

Instance Method Details

#association?Boolean

Is this an association

Returns:

  • (Boolean)


289
290
291
# File 'lib/rails_admin/config/fields/base.rb', line 289

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

#eager_load_valuesObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/rails_admin/config/fields/base.rb', line 269

def eager_load_values
  case eager_load
  when true
    [name]
  when false, nil
    []
  else
    Array.wrap(eager_load)
  end
end

#editable?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/rails_admin/config/fields/base.rb', line 284

def editable?
  !((@properties && @properties.read_only?) || (bindings[:object] && bindings[:object].readonly?))
end

#errorsObject

Reader for validation errors of the bound object



294
295
296
297
298
# File 'lib/rails_admin/config/fields/base.rb', line 294

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

#filter_optionsObject



101
102
103
104
105
106
107
108
109
# File 'lib/rails_admin/config/fields/base.rb', line 101

def filter_options
  {
    label: label,
    name: name,
    operator: default_filter_operator,
    operators: filter_operators,
    type: type,
  }
end

#form_default_valueObject



381
382
383
# File 'lib/rails_admin/config/fields/base.rb', line 381

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

#form_valueObject



385
386
387
# File 'lib/rails_admin/config/fields/base.rb', line 385

def form_value
  form_default_value.nil? ? formatted_value : form_default_value
end

#generic_field_helpObject



358
359
360
361
362
363
# File 'lib/rails_admin/config/fields/base.rb', line 358

def generic_field_help
  model = abstract_model.model_name.underscore
  model_lookup = :"admin.help.#{model}.#{name}"
  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



354
355
356
# File 'lib/rails_admin/config/fields/base.rb', line 354

def generic_help
  "#{required? ? I18n.translate('admin.form.required') : I18n.translate('admin.form.optional')}. "
end

#inverse_ofObject



373
374
375
# File 'lib/rails_admin/config/fields/base.rb', line 373

def inverse_of
  nil
end

#method_nameObject



377
378
379
# File 'lib/rails_admin/config/fields/base.rb', line 377

def method_name
  name
end

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

Inverse accessor whether this field is required.

See Also:

  • :required?


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

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

#optional=(state) ⇒ Object

Writer to make field optional.

See Also:

  • optional


321
322
323
# File 'lib/rails_admin/config/fields/base.rb', line 321

def optional=(state)
  optional(state)
end

#optional?Boolean

Reader whether field is optional.

Returns:

  • (Boolean)

See Also:

  • :required?


303
304
305
# File 'lib/rails_admin/config/fields/base.rb', line 303

def optional?
  !required?
end

#parse_input(_params) ⇒ Object



369
370
371
# File 'lib/rails_admin/config/fields/base.rb', line 369

def parse_input(_params)
  # overridden
end

#parse_value(value) ⇒ Object



365
366
367
# File 'lib/rails_admin/config/fields/base.rb', line 365

def parse_value(value)
  value
end

#sort_columnObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails_admin/config/fields/base.rb', line 63

def sort_column
  if sortable == true
    "#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(name)}"
  elsif (sortable.is_a?(String) || sortable.is_a?(Symbol)) && sortable.to_s.include?('.') # just provide sortable, don't do anything smart
    sortable
  elsif sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
    "#{sortable.keys.first}.#{sortable.values.first}"
  elsif association? # use column on target table
    "#{associated_model_config.abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
  else # use described column in the field conf.
    "#{abstract_model.quoted_table_name}.#{abstract_model.quote_column_name(sortable)}"
  end
end

#typeObject

Reader for field’s type



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

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

#type_css_classObject



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

def type_css_class
  "#{type}_type"
end

#valueObject

Reader for field’s value



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rails_admin/config/fields/base.rb', line 331

def value
  bindings[:object].safe_send(name)
rescue NoMethodError => e
  raise e.exception <<~ERROR
    #{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
  ERROR
end

#virtual?Boolean

Returns:

  • (Boolean)


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

def virtual?
  properties.blank?
end