Class: ActiveElement::Components::Form
- Inherits:
-
Object
- Object
- ActiveElement::Components::Form
- 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
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Instance Method Summary collapse
- #class_name ⇒ Object
- #default_record_value(field, default) ⇒ Object
- #display_value_for_select(field, options) ⇒ Object
- #form_value_mapping_value(field) ⇒ Object
- #full_error_message ⇒ Object
-
#initialize(controller, fields:, submit:, item:, title: nil, destroy: false, search: false, modal: false, columns: 1, **kwargs) ⇒ Form
constructor
rubocop:disable Metrics/MethodLength.
-
#locals ⇒ Object
rubocop:disable Metrics/MethodLength.
- #model ⇒ Object
- #name_for(form, schema_field, type:) ⇒ Object
- #nested_value(field) ⇒ Object
- #options_for_json_array_field(options) ⇒ Object
- #options_for_select(field, field_options) ⇒ Object
- #record ⇒ Object
- #schema_for(field, options) ⇒ Object
- #tabindex ⇒ Object
-
#template ⇒ Object
rubocop:enable Metrics/MethodLength.
- #valid?(field = nil) ⇒ Boolean
- #value_for(*field, default: nil) ⇒ Object
- #value_for_json_array_field(field, schema_field, element_index = nil) ⇒ Object
Methods included from Translations
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
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
10 11 12 |
# File 'lib/active_element/components/form.rb', line 10 def controller @controller end |
#search ⇒ Object (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_name ⇒ Object
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, ) (field, ).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_message ⇒ Object
68 69 70 |
# File 'lib/active_element/components/form.rb', line 68 def record&.errors&.&.join(', ') end |
#locals ⇒ Object
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., 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 |
#model ⇒ Object
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 () .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 (field, ) return [['', '']] + (field, ) unless [:blank] == false (field, ) end |
#record ⇒ Object
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, ) .key?(:schema) ? .fetch(:schema) : Util.json_schema(model: record.class, field: field) end |
#tabindex ⇒ Object
138 139 140 |
# File 'lib/active_element/components/form.rb', line 138 def tabindex (@tabindex += 1) end |
#template ⇒ Object
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
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 |