Skip to content

Commit 055b1e6

Browse files
committed
DRY up id generation
1 parent 35f432c commit 055b1e6

File tree

7 files changed

+13
-18
lines changed

7 files changed

+13
-18
lines changed

lib/bootstrap_form/components/labels.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def prepare_label_options(id, name, options, custom_label_col, group_layout)
5252

5353
options[:class] = label_classes(name, options, custom_label_col, group_layout)
5454
options.delete(:class) if options[:class].none?
55-
options[:id] = id.present? ? "#{id}_feedback" : field_id(name, :feedback) if error?(name) && label_errors
55+
options[:id] = aria_feedback_id(name:, id:) if error?(name) && label_errors
5656
end
5757
end
5858
end

lib/bootstrap_form/components/validation.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ def generate_error(name, id)
6767
help_text = get_error_messages(name)
6868
help_klass = "invalid-feedback"
6969
help_tag = :div
70-
id = id.present? ? "#{id}_feedback" : field_id(name, :feedback)
7170

72-
content_tag(help_tag, help_text, class: help_klass, id:)
71+
content_tag(help_tag, help_text, class: help_klass, id: aria_feedback_id(id:, name:))
7372
end
7473

7574
def get_error_messages(name)
@@ -85,6 +84,10 @@ def get_error_messages(name)
8584
safe_join(object.errors[name], ", ")
8685
end
8786
# rubocop:enable Metrics/AbcSize
87+
88+
def aria_feedback_id(name:, id: nil)
89+
id.present? ? "#{id}_feedback" : field_id(name, :feedback)
90+
end
8891
end
8992
end
9093
end

lib/bootstrap_form/form_group_builder.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ def form_group_css_options(method, html_options, options)
100100
css_options[:class] = safe_join([control_classes, css_options[:class]].compact, " ")
101101
if error?(method)
102102
css_options[:class] << " is-invalid"
103-
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(method, :feedback)
104-
css_options[:aria] = { labelledby: }
103+
css_options[:aria] = { labelledby: aria_feedback_id(id: options[:id], name: method) }
105104
end
106105
css_options[:placeholder] = form_group_placeholder(options, method) if options[:label_as_placeholder]
107106
css_options

lib/bootstrap_form/helpers/bootstrap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def errors_on(name, options={})
3636

3737
tag.div(
3838
class: custom_class || "invalid-feedback",
39-
id: options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
39+
id: aria_feedback_id(id: options[:id], name:)
4040
) do
4141
errors = if hide_attribute_name
4242
object.errors[name]

lib/bootstrap_form/inputs/check_box.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ def check_box_options(name, options)
4141
:inline, :label, :label_class, :label_col, :layout, :skip_label,
4242
:switch, :wrapper, :wrapper_class)
4343
check_box_options[:class] = check_box_classes(name, options)
44-
if error?(name)
45-
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
46-
check_box_options[:aria] = { labelledby: }
47-
end
44+
check_box_options[:aria] = { labelledby: aria_feedback_id(id: options[:id], name:) } if error?(name)
4845
check_box_options.merge!(required_field_options(options, name))
4946
end
5047

lib/bootstrap_form/inputs/inputs_collection.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def field_group(name, options, &)
8989
:add_control_col_class, :append, :control_col, :floating, :help, :icon, :id,
9090
:input_group_class, :label, :label_col, :layout, :prepend
9191
),
92-
aria: { labelledby: options[:id] || default_id(name) },
92+
aria: { labelledby: group_label_div_id(id: options[:id], name:) },
9393
role: :group
9494
) do
9595
group_label_div = generate_group_label_div(name, options)
@@ -100,15 +100,14 @@ def field_group(name, options, &)
100100

101101
def generate_group_label_div(name, options)
102102
group_label_div_class = options.dig(:label, :class) || "form-label"
103-
id = options[:id] || default_id(name)
104103

105104
tag.div(
106105
**{ class: group_label_div_class }.compact,
107-
id:
106+
id: group_label_div_id(id: options[:id], name:)
108107
) { label_text(name, options.dig(:label, :text)) }
109108
end
110109

111-
def default_id(name) = raw("#{object_name}_#{name}") # rubocop:disable Rails/OutputSafety
110+
def group_label_div_id(id:, name:) = id || field_id(name)
112111
end
113112
end
114113
end

lib/bootstrap_form/inputs/radio_button.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def radio_button_options(name, options)
2828
radio_button_options = options.except(:class, :label, :label_class, :error_message, :help,
2929
:inline, :hide_label, :skip_label, :wrapper, :wrapper_class)
3030
radio_button_options[:class] = radio_button_classes(name, options)
31-
if error?(name)
32-
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
33-
radio_button_options[:aria] = { labelledby: }
34-
end
31+
radio_button_options[:aria] = { labelledby: aria_feedback_id(id: options[:id], name:) } if error?(name)
3532
radio_button_options.merge!(required_field_options(options, name))
3633
end
3734

0 commit comments

Comments
 (0)