app/components/fox_tail/concerns/formable.rb in fox_tail-0.1.1 vs app/components/fox_tail/concerns/formable.rb in fox_tail-0.2.0

- old
+ new

@@ -2,19 +2,26 @@ module FoxTail::Concerns::Formable include ActionView::ModelNaming extend ActiveSupport::Concern + FORM_OPTIONS = %i[ + object_name method_name namespace object + skip_default_ids allow_method_names_outside_object + value_array object_index + ].freeze + included do has_option :object_name has_option :method_name has_option :namespace has_option :object has_option :skip_default_ids, type: :boolean, default: false has_option :allow_method_names_outside_object, type: :boolean, default: false has_option :value_array, type: :boolean, default: false has_option :object_index + has_option :errors_for end def initialize(*) super @@ -33,11 +40,11 @@ @auto_index = nil end end def name_and_id_index - if self.options.key? :object_index + if options.key? :object_index object_index.presence || "" elsif @generate_indexed_names @auto_index || "" end end @@ -77,11 +84,11 @@ end end 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 + attributes[:id] = tag_id_for_value value, attributes: attributes unless attributes.key? :id end def tag_id_for_value(value, attributes = html_attributes) if value.nil? tag_id @@ -114,15 +121,15 @@ else attributes[:id] = tag_id end end - def field_id(method, *suffixes, namespace: self.namespace, index: self.object_index) + 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 - def field_name(method, *methods, multiple: false, index: self.object_index) + 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 def tag_name field_name sanitized_method_name, multiple: value_array?, index: name_and_id_index @@ -139,18 +146,25 @@ def sanitized_value(value) value.to_s.gsub(/[\s.]/, "_").gsub(/[^-[[:word:]]]/, "").downcase end def translator(value: nil, scope: nil, default: "") - FoxTail::Translator.new object, - object_name, - method_name, - value: value, - scope: scope, - default: default + FoxTail::Translator.new object, object_name, method_name, value: value, scope: scope, default: default end - def object_errors?(method = method_name) + def object_errors_for + (Array(method_name) + Array(options[:errors_for])).compact_blank.uniq + end + + def object_errors?(methods = object_errors_for) + return false if methods.blank? + object = convert_to_model self.object - object.present? && method.present? && object.errors[method.to_sym].present? + return false if object.blank? + + methods.any? { |m| object.errors[m].present? } + end + + def objectify_options(options) + options.merge self.options.slice(*FORM_OPTIONS) end end