Class: RailsAdmin::Config::Fields::Base
- Inherits:
-
Object
- Object
- RailsAdmin::Config::Fields::Base
- Includes:
- Configurable, Groupable, Hideable, Inspectable, Proxyable
- Defined in:
- lib/rails_admin/config/fields/base.rb
Overview
rubocop:disable Metrics/ClassLength
Direct Known Subclasses
Association, Types::Boolean, Types::Datetime, Types::Enum, Types::FileUpload, Types::Inet, Types::MultipleFileUpload, Types::Numeric, Types::StringLike
Constant Summary collapse
- NAMED_INSTANCE_VARIABLES =
%i[ @parent @root @section @children_fields_registered @associated_model_config @group ].freeze
Instance Attribute Summary collapse
-
#abstract_model ⇒ Object
readonly
Returns the value of attribute abstract_model.
-
#defined ⇒ Object
Returns the value of attribute defined.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#order ⇒ Object
Returns the value of attribute order.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#section ⇒ Object
Returns the value of attribute section.
Instance Method Summary collapse
-
#association? ⇒ Boolean
Is this an association.
- #eager_load_values ⇒ Object
- #editable? ⇒ Boolean
-
#errors ⇒ Object
Reader for validation errors of the bound object.
- #filter_options ⇒ Object
- #form_default_value ⇒ Object
- #form_value ⇒ Object
- #generic_field_help ⇒ Object
- #generic_help ⇒ Object
-
#initialize(parent, name, properties) ⇒ Base
constructor
A new instance of Base.
- #inverse_of ⇒ Object
- #method_name ⇒ Object
-
#optional(state = nil, &block) ⇒ Object
Inverse accessor whether this field is required.
-
#optional=(state) ⇒ Object
Writer to make field optional.
-
#optional? ⇒ Boolean
Reader whether field is optional.
- #parse_input(_params) ⇒ Object
- #parse_value(value) ⇒ Object
- #sort_column ⇒ Object
-
#type ⇒ Object
Reader for field’s type.
- #type_css_class ⇒ Object
-
#value ⇒ Object
Reader for field’s value.
- #virtual? ⇒ Boolean
Methods included from Inspectable
Methods included from Groupable
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
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_model ⇒ Object (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 |
#defined ⇒ Object
Returns the value of attribute defined.
20 21 22 |
# File 'lib/rails_admin/config/fields/base.rb', line 20 def defined @defined end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/rails_admin/config/fields/base.rb', line 19 def name @name end |
#order ⇒ Object
Returns the value of attribute order.
20 21 22 |
# File 'lib/rails_admin/config/fields/base.rb', line 20 def order @order end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
19 20 21 |
# File 'lib/rails_admin/config/fields/base.rb', line 19 def parent @parent end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
19 20 21 |
# File 'lib/rails_admin/config/fields/base.rb', line 19 def properties @properties end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
19 20 21 |
# File 'lib/rails_admin/config/fields/base.rb', line 19 def root @root end |
#section ⇒ Object
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
289 290 291 |
# File 'lib/rails_admin/config/fields/base.rb', line 289 def association? is_a?(RailsAdmin::Config::Fields::Association) end |
#eager_load_values ⇒ Object
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
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 |
#errors ⇒ Object
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_options ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/rails_admin/config/fields/base.rb', line 101 def { label: label, name: name, operator: default_filter_operator, operators: filter_operators, type: type, } end |
#form_default_value ⇒ Object
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_value ⇒ Object
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_help ⇒ Object
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_help ⇒ Object
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_of ⇒ Object
373 374 375 |
# File 'lib/rails_admin/config/fields/base.rb', line 373 def inverse_of nil end |
#method_name ⇒ Object
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.
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.
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.
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_column ⇒ Object
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 |
#type ⇒ Object
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_class ⇒ Object
43 44 45 |
# File 'lib/rails_admin/config/fields/base.rb', line 43 def type_css_class "#{type}_type" end |
#value ⇒ Object
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.} 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
47 48 49 |
# File 'lib/rails_admin/config/fields/base.rb', line 47 def virtual? properties.blank? end |