Module: ViewComponent::Form::Helpers::Rails7Backports

Included in:
Builder
Defined in:
lib/view_component/form/helpers/rails_7_backports.rb

Instance Method Summary collapse

Instance Method Details

#field_id(method_name, *suffixes, namespace: @options[:namespace], index: @index) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/view_component/form/helpers/rails_7_backports.rb', line 7

def field_id(method_name, *suffixes, namespace: @options[:namespace], index: @index)
  object_name = object_name.model_name.singular if object_name.respond_to?(:model_name)

  sanitized_object_name = object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").delete_suffix("_")

  sanitized_method_name = method_name.to_s.delete_suffix("?")

  [
    namespace,
    sanitized_object_name.presence,
    (index unless sanitized_object_name.empty?),
    sanitized_method_name,
    *suffixes
  ].tap(&:compact!).join("_")
end