Module: FoxTail::Concerns::Formable
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionView::ModelNaming
- Included in:
- AutocompleteComponent, ButtonBaseComponent, ButtonGroupComponent, FoxTail::CheckboxComponent, Dropdown::InputItemComponent, DropzoneComponent, HelperTextComponent, InputBaseComponent, InputErrorListComponent, InputGroupComponent, LabelComponent, RadioButtonComponent, RangeComponent, ShowPasswordComponent
- Defined in:
- app/components/fox_tail/concerns/formable.rb
Constant Summary collapse
- FORM_OPTIONS =
i[ object_name method_name namespace object skip_default_ids allow_method_names_outside_object value_array object_index ].freeze
Instance Method Summary collapse
- #add_default_id(attributes: html_attributes) ⇒ Object
- #add_default_name(attributes: html_attributes) ⇒ Object
- #add_default_name_and_id(attributes: html_attributes) ⇒ Object
- #add_default_name_and_id_for_value(value, attributes: html_attributes) ⇒ Object
- #field_id(method, *suffixes, namespace: self.namespace, index: object_index) ⇒ Object
- #field_name(method, *methods, multiple: false, index: object_index) ⇒ Object
- #initialize ⇒ Object
- #name_and_id_index ⇒ Object
- #object_errors?(methods = object_errors_for) ⇒ Boolean
- #object_errors_for ⇒ Object
- #objectify_options(options) ⇒ Object
- #retrieve_autoindex ⇒ Object
- #sanitized_method_name ⇒ Object
- #sanitized_value(value) ⇒ Object
- #tag_id ⇒ Object
- #tag_id_for_value(value, attributes = html_attributes) ⇒ Object
- #tag_name ⇒ Object
- #translator(value: nil, scope: nil, default: "") ⇒ Object
- #value_before_type_cast ⇒ Object
- #value_came_from_user? ⇒ Boolean
- #value_from_object ⇒ Object
Instance Method Details
#add_default_id(attributes: html_attributes) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'app/components/fox_tail/concerns/formable.rb', line 116 def add_default_id(attributes: html_attributes) return if skip_default_ids? if attributes.key? :id attributes[:id] = attributes[:id] ? "#{namespace}_#{attributes[:id]}" : namespace if namespace? else attributes[:id] = tag_id end end |
#add_default_name(attributes: html_attributes) ⇒ Object
112 113 114 |
# File 'app/components/fox_tail/concerns/formable.rb', line 112 def add_default_name(attributes: html_attributes) attributes[:name] = tag_name unless attributes.key? :name end |
#add_default_name_and_id(attributes: html_attributes) ⇒ Object
107 108 109 110 |
# File 'app/components/fox_tail/concerns/formable.rb', line 107 def add_default_name_and_id(attributes: html_attributes) add_default_name attributes: attributes add_default_id attributes: attributes end |
#add_default_name_and_id_for_value(value, attributes: html_attributes) ⇒ Object
87 88 89 90 |
# File 'app/components/fox_tail/concerns/formable.rb', line 87 def add_default_name_and_id_for_value(value, attributes: html_attributes) add_default_name attributes: attributes attributes[:id] = tag_id_for_value value, attributes: attributes unless attributes.key? :id end |
#field_id(method, *suffixes, namespace: self.namespace, index: object_index) ⇒ Object
126 127 128 |
# File 'app/components/fox_tail/concerns/formable.rb', line 126 def field_id(method, *suffixes, namespace: self.namespace, index: object_index) view_context.field_id(object_name, method, *suffixes, namespace: namespace.presence, index: index.presence).presence end |
#field_name(method, *methods, multiple: false, index: object_index) ⇒ Object
130 131 132 |
# File 'app/components/fox_tail/concerns/formable.rb', line 130 def field_name(method, *methods, multiple: false, index: object_index) view_context.field_name(object_name, method, *methods, index: index.presence, multiple: !!multiple).presence end |
#initialize ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/components/fox_tail/concerns/formable.rb', line 25 def initialize(*) super [:object_name] = object_name.to_s.dup if object_name? [:method_name] = method_name.to_s.dup if method_name? return unless object_name? object_name.sub!(/\[\]$/, "") || object_name.sub!(/\[\]\]$/, "]") if Regexp.last_match @generate_indexed_names = true @auto_index = retrieve_autoindex else @generate_indexed_names = false @auto_index = nil end end |
#name_and_id_index ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/components/fox_tail/concerns/formable.rb', line 44 def name_and_id_index if .key? :object_index object_index.presence || "" elsif @generate_indexed_names @auto_index || "" end end |
#object_errors?(methods = object_errors_for) ⇒ Boolean
158 159 160 161 162 163 164 165 |
# File 'app/components/fox_tail/concerns/formable.rb', line 158 def object_errors?(methods = object_errors_for) return false if methods.blank? object = convert_to_model self.object return false if object.blank? methods.any? { |m| object.errors[m].present? } end |
#object_errors_for ⇒ Object
154 155 156 |
# File 'app/components/fox_tail/concerns/formable.rb', line 154 def object_errors_for (Array(method_name) + Array([:errors_for])).compact_blank.uniq end |
#objectify_options(options) ⇒ Object
167 168 169 |
# File 'app/components/fox_tail/concerns/formable.rb', line 167 def () .merge self..slice(*FORM_OPTIONS) end |
#retrieve_autoindex ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'app/components/fox_tail/concerns/formable.rb', line 65 def retrieve_autoindex unless object.respond_to?(:to_param) raise ArgumentError, " object[] naming but object param and @object var don't exist or don't respond to to_param: \#{object.inspect}\n MSG\n end\n\n object.to_param\nend\n" |
#sanitized_method_name ⇒ Object
142 143 144 |
# File 'app/components/fox_tail/concerns/formable.rb', line 142 def sanitized_method_name @sanitized_method_name ||= method_name.presence&.delete_suffix("?") end |
#sanitized_value(value) ⇒ Object
146 147 148 |
# File 'app/components/fox_tail/concerns/formable.rb', line 146 def sanitized_value(value) value.to_s.gsub(/[\s.]/, "_").gsub(/[^-[[:word:]]]/, "").downcase end |
#tag_id ⇒ Object
138 139 140 |
# File 'app/components/fox_tail/concerns/formable.rb', line 138 def tag_id field_id method_name, index: name_and_id_index, namespace: namespace end |
#tag_id_for_value(value, attributes = html_attributes) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/components/fox_tail/concerns/formable.rb', line 92 def tag_id_for_value(value, attributes = html_attributes) if value.nil? tag_id else specified_id = attributes[:id] id = tag_id if specified_id.blank? && id.present? "#{id}_#{sanitized_value(tag_value)}" else tag_id end end end |
#tag_name ⇒ Object
134 135 136 |
# File 'app/components/fox_tail/concerns/formable.rb', line 134 def tag_name field_name sanitized_method_name, multiple: value_array?, index: name_and_id_index end |
#translator(value: nil, scope: nil, default: "") ⇒ Object
150 151 152 |
# File 'app/components/fox_tail/concerns/formable.rb', line 150 def translator(value: nil, scope: nil, default: "") FoxTail::Translator.new object, object_name, method_name, value: value, scope: scope, default: default end |
#value_before_type_cast ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/components/fox_tail/concerns/formable.rb', line 75 def value_before_type_cast return if object.nil? method_before_type_cast = "#{method_name}_before_type_cast" if value_came_from_user? && object.respond_to?(method_before_type_cast) object.public_send(method_before_type_cast) else value_from_object end end |
#value_came_from_user? ⇒ Boolean
60 61 62 63 |
# File 'app/components/fox_tail/concerns/formable.rb', line 60 def value_came_from_user? method_came_from_user = "#{method_name}_came_from_user?" !object.respond_to?(method_came_from_user) || object.public_send(method_came_from_user) end |
#value_from_object ⇒ Object
52 53 54 55 56 57 58 |
# File 'app/components/fox_tail/concerns/formable.rb', line 52 def value_from_object if @allow_method_names_outside_object object.public_send method_name if object.respond_to?(method_name) else object&.public_send method_name end end |