Class: ActiveElement::Components::Form

Inherits:
Object
  • Object
show all
Includes:
Translations
Defined in:
lib/active_element/components/form.rb

Overview

A form component for rendering a standard form with various inputs in a uniform manner, includes validation errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Translations

#i18n

Constructor Details

#initialize(controller, fields:, submit:, item:, title: nil, destroy: false, search: false, modal: false, columns: 1, **kwargs) ⇒ Form

rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_element/components/form.rb', line 13

def initialize(controller, fields:, submit:, item:, title: nil, destroy: false, search: false,
               modal: false, columns: 1, **kwargs)
  @controller = controller
  @fields = fields
  @title = title
  @submit = submit
  @destroy = destroy
  @item = item
  @modal = modal
  @kwargs = kwargs
  @model = model
  @columns = columns
  @search = search
  @action = kwargs.delete(:action) { default_action }
  @method = kwargs.delete(:method) { default_method }.to_s.downcase.to_sym
  @tabindex = 1
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



10
11
12
# File 'lib/active_element/components/form.rb', line 10

def controller
  @controller
end

#searchObject (readonly)

Returns the value of attribute search.



10
11
12
# File 'lib/active_element/components/form.rb', line 10

def search
  @search
end

Instance Method Details

#class_nameObject



58
59
60
# File 'lib/active_element/components/form.rb', line 58

def class_name
  [default_class_name, kwargs.fetch(:class, nil)].compact.join(' ')
end

#default_record_value(field, default) ⇒ Object



113
114
115
# File 'lib/active_element/components/form.rb', line 113

def default_record_value(field, default)
  record&.public_send(field).presence || default
end

#display_value_for_select(field, options) ⇒ Object



91
92
93
94
95
# File 'lib/active_element/components/form.rb', line 91

def display_value_for_select(field, options)
  options_for_select(field, options).find do |_display_value, value|
    value == value_for(field)
  end&.first
end

#form_value_mapping_value(field) ⇒ Object



109
110
111
# File 'lib/active_element/components/form.rb', line 109

def form_value_mapping_value(field)
  Util::FormValueMapping.new(component: self, record: record, field: field).value
end

#full_error_messageObject



68
69
70
# File 'lib/active_element/components/form.rb', line 68

def full_error_message
  record&.errors&.full_messages&.join(', ')
end

#localsObject

rubocop:disable Metrics/MethodLength



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_element/components/form.rb', line 36

def locals # rubocop:disable Metrics/MethodLength
  {
    component: self,
    fields: form_field_mapping.fields_with_types_and_options,
    record: record,
    submit_label: submit_label,
    submit_position: submit_position,
    class_name: class_name,
    becomes_model: becomes_model,
    method: method,
    action: action,
    kwargs: kwargs,
    model_param: model_param,
    record_path: record_path,
    destroy: destroy,
    modal: modal,
    columns: columns,
    title: title,
    id: form_id
  }
end

#modelObject



134
135
136
# File 'lib/active_element/components/form.rb', line 134

def model
  record&.class
end

#name_for(form, schema_field, type:) ⇒ Object



82
83
84
85
# File 'lib/active_element/components/form.rb', line 82

def name_for(form, schema_field, type:)
  base = "#{form.object_name}[#{schema_field.fetch(:name)}]"
  type == :array ? "#{base}[]" : base
end

#nested_value(field) ⇒ Object



105
106
107
# File 'lib/active_element/components/form.rb', line 105

def nested_value(field)
  field.reduce(item) { |hash, key| hash.fetch(key, {}) }
end

#options_for_json_array_field(options) ⇒ Object



117
118
119
# File 'lib/active_element/components/form.rb', line 117

def options_for_json_array_field(options)
  options.map { |option| option.is_a?(Array) ? option : [option, option] }
end

#options_for_select(field, field_options) ⇒ Object



62
63
64
65
66
# File 'lib/active_element/components/form.rb', line 62

def options_for_select(field, field_options)
  return [['', '']] + base_options_for_select(field, field_options) unless field_options[:blank] == false

  base_options_for_select(field, field_options)
end

#recordObject



128
129
130
131
132
# File 'lib/active_element/components/form.rb', line 128

def record
  return nil if kwargs.fetch(:model, nil).blank?

  kwargs[:model].is_a?(Array) ? kwargs[:model].last : kwargs[:model]
end

#schema_for(field, options) ⇒ Object



87
88
89
# File 'lib/active_element/components/form.rb', line 87

def schema_for(field, options)
  options.key?(:schema) ? options.fetch(:schema) : Util.json_schema(model: record.class, field: field)
end

#tabindexObject



138
139
140
# File 'lib/active_element/components/form.rb', line 138

def tabindex
  (@tabindex += 1)
end

#templateObject

rubocop:enable Metrics/MethodLength



32
33
34
# File 'lib/active_element/components/form.rb', line 32

def template
  'active_element/components/form'
end

#valid?(field = nil) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/active_element/components/form.rb', line 72

def valid?(field = nil)
  return true if record.blank? || !record.changed?

  record.valid?

  return record&.errors.blank? if field.nil?

  valid_field?(field)
end

#value_for(*field, default: nil) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/active_element/components/form.rb', line 97

def value_for(*field, default: nil)
  return form_value_mapping_value(field.first) if record.class.is_a?(ActiveModel::Naming)
  return default_record_value(field.first, default) if record.present? && record.respond_to?(field.first)
  return nested_value(field).presence || default if item.present?

  default
end

#value_for_json_array_field(field, schema_field, element_index = nil) ⇒ Object



121
122
123
124
125
126
# File 'lib/active_element/components/form.rb', line 121

def value_for_json_array_field(field, schema_field, element_index = nil)
  array = value_for(field, default: {}).fetch(schema_field[:name], [])
  return array if element_index.nil?

  array.fetch(element_index, nil)
end