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.
- #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
-
#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.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rails_admin/config/fields/base.rb', line 25 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.
17 18 19 |
# File 'lib/rails_admin/config/fields/base.rb', line 17 def abstract_model @abstract_model end |
#defined ⇒ Object
Returns the value of attribute defined.
18 19 20 |
# File 'lib/rails_admin/config/fields/base.rb', line 18 def defined @defined end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/rails_admin/config/fields/base.rb', line 17 def name @name end |
#order ⇒ Object
Returns the value of attribute order.
18 19 20 |
# File 'lib/rails_admin/config/fields/base.rb', line 18 def order @order end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
17 18 19 |
# File 'lib/rails_admin/config/fields/base.rb', line 17 def parent @parent end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
17 18 19 |
# File 'lib/rails_admin/config/fields/base.rb', line 17 def properties @properties end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
17 18 19 |
# File 'lib/rails_admin/config/fields/base.rb', line 17 def root @root end |
#section ⇒ Object
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
259 260 261 |
# File 'lib/rails_admin/config/fields/base.rb', line 259 def association? is_a?(RailsAdmin::Config::Fields::Association) end |
#eager_load_values ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rails_admin/config/fields/base.rb', line 239 def eager_load_values case eager_load when true [name] when false, nil [] else Array.wrap(eager_load) end end |
#editable? ⇒ Boolean
254 255 256 |
# File 'lib/rails_admin/config/fields/base.rb', line 254 def editable? !((@properties && @properties.read_only?) || (bindings[:object] && bindings[:object].readonly?)) end |
#errors ⇒ Object
Reader for validation errors of the bound object
264 265 266 267 268 |
# File 'lib/rails_admin/config/fields/base.rb', line 264 def errors ([name] + children_fields).uniq.collect do |column_name| bindings[:object].errors[column_name] end.uniq.flatten end |
#form_default_value ⇒ Object
351 352 353 |
# File 'lib/rails_admin/config/fields/base.rb', line 351 def form_default_value (default_value if bindings[:object].new_record? && value.nil?) end |
#form_value ⇒ Object
355 356 357 |
# File 'lib/rails_admin/config/fields/base.rb', line 355 def form_value form_default_value.nil? ? formatted_value : form_default_value end |
#generic_field_help ⇒ Object
328 329 330 331 332 333 |
# File 'lib/rails_admin/config/fields/base.rb', line 328 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_help ⇒ Object
324 325 326 |
# File 'lib/rails_admin/config/fields/base.rb', line 324 def generic_help "#{required? ? I18n.translate('admin.form.required') : I18n.translate('admin.form.optional')}. " end |
#inverse_of ⇒ Object
343 344 345 |
# File 'lib/rails_admin/config/fields/base.rb', line 343 def inverse_of nil end |
#method_name ⇒ Object
347 348 349 |
# File 'lib/rails_admin/config/fields/base.rb', line 347 def method_name name end |
#optional(state = nil, &block) ⇒ Object
Inverse accessor whether this field is required.
280 281 282 283 284 285 286 |
# File 'lib/rails_admin/config/fields/base.rb', line 280 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.
291 292 293 |
# File 'lib/rails_admin/config/fields/base.rb', line 291 def optional=(state) optional(state) end |
#optional? ⇒ Boolean
Reader whether field is optional.
273 274 275 |
# File 'lib/rails_admin/config/fields/base.rb', line 273 def optional? !required? end |
#parse_input(_params) ⇒ Object
339 340 341 |
# File 'lib/rails_admin/config/fields/base.rb', line 339 def parse_input(_params) # overriden end |
#parse_value(value) ⇒ Object
335 336 337 |
# File 'lib/rails_admin/config/fields/base.rb', line 335 def parse_value(value) value end |
#type ⇒ Object
Reader for field’s type
296 297 298 |
# File 'lib/rails_admin/config/fields/base.rb', line 296 def type @type ||= self.class.name.to_s.demodulize.underscore.to_sym end |
#type_css_class ⇒ Object
41 42 43 |
# File 'lib/rails_admin/config/fields/base.rb', line 41 def type_css_class "#{type}_type" end |
#value ⇒ Object
Reader for field’s value
301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/rails_admin/config/fields/base.rb', line 301 def value bindings[:object].safe_send(name) rescue NoMethodError => e raise e.exception <<-ERROR.gsub(/^\s{10}/, '') #{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
45 46 47 |
# File 'lib/rails_admin/config/fields/base.rb', line 45 def virtual? properties.blank? end |