Class: ActiveElement::Components::Util::FormFieldMapping

Inherits:
Object
  • Object
show all
Includes:
EmailFields, PhoneFields, SecretFields
Defined in:
lib/active_element/components/util/form_field_mapping.rb

Overview

Normalizes Form ‘fields` parameter from various supported input formats.

Constant Summary

Constants included from EmailFields

EmailFields::EMAIL_FIELDS

Constants included from PhoneFields

PhoneFields::PHONE_FIELDS

Constants included from SecretFields

SecretFields::SECRET_FIELDS

Instance Method Summary collapse

Methods included from EmailFields

#email_field?

Methods included from PhoneFields

#phone_field?

Methods included from SecretFields

#secret_field?

Constructor Details

#initialize(record:, fields:, controller:, i18n:, search: false) ⇒ FormFieldMapping

Returns a new instance of FormFieldMapping.



12
13
14
15
16
17
18
# File 'lib/active_element/components/util/form_field_mapping.rb', line 12

def initialize(record:, fields:, controller:, i18n:, search: false)
  @controller = controller
  @record = record || default_record
  @fields = fields
  @i18n = i18n
  @search = search
end

Instance Method Details

#fields_with_types_and_optionsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_element/components/util/form_field_mapping.rb', line 20

def fields_with_types_and_options
  compiled_fields = fields.map do |field|
    next field_with_default_type_and_default_options(field) unless field.is_a?(Array)
    next field_with_provided_type_and_provided_options(field) if normalized_field?(field)
    next field_with_default_type_and_provided_options(field) if field_name_with_options?(field)
    next field_with_type(field) if field_name_with_type?(field)

    raise_unrecognized_field_format(field)
  end

  fields_with_default_label(compiled_fields)
end