From d719b2291e220d85eb3d05d3f010af2e0c426ea0 Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 17 Mar 2022 10:35:52 +0100 Subject: [PATCH 1/2] Do not generate an empty hint element when there's an empty hint --- lib/consul_form_builder.rb | 4 ++-- spec/lib/consul_form_builder_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index e9d59dcd2..d01174458 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -76,13 +76,13 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder end def help_text(attribute, options) - if options[:hint] + if options[:hint].present? tag.span options[:hint], class: "help-text", id: help_text_id(attribute, options) end end def help_text_id(attribute, options) - if options[:hint] + if options[:hint].present? "#{custom_label(attribute, nil, nil).match(/for="([^"]+)"/)[1]}-help-text" end end diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index a61e984da..ed896368f 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -28,6 +28,13 @@ describe ConsulFormBuilder do expect(page).to have_css "input[aria-describedby='dummy_title-help-text']" end + it "does not generate empty hints" do + render builder.text_field(:title, hint: "") + + expect(page).not_to have_css ".help-text" + expect(page).not_to have_css "input[aria-describedby]" + end + it "does not generate a hint attribute" do render builder.text_field(:title) From 1fa05b1f547073446d27df743c610264f3090517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 25 Mar 2022 15:23:09 +0100 Subject: [PATCH 2/2] Fix crash on attributes with interpolation arguments The application crashed when we generated hints to attributes with interpolation arguments in their `human_attribute_name`. When generating the hint, we used the `custom_label` method to generate a label and get the `for` attribute and, since we weren't passing a text, it used the default human attribute name for the field. However, it crashes if the default attribute name requires an interpolation argument. So now, since we were only using the `custom_label` method in order to get the `for` attribute, we're simply passing an arbitrary text to the method. --- lib/consul_form_builder.rb | 2 +- spec/lib/consul_form_builder_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index d01174458..5eb292079 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -83,7 +83,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder def help_text_id(attribute, options) if options[:hint].present? - "#{custom_label(attribute, nil, nil).match(/for="([^"]+)"/)[1]}-help-text" + "#{custom_label(attribute, "Example", nil).match(/for="([^"]+)"/)[1]}-help-text" end end end diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index ed896368f..7cc39d7c5 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -40,6 +40,18 @@ describe ConsulFormBuilder do expect(page).not_to have_css "input[hint]" end + + describe "attributes requiring interpolation parameters" do + before { I18n.backend.store_translations(:en, { attributes: { title: "Title %{info}" }}) } + after { I18n.backend.reload! } + + it "generates a hint" do + render builder.text_field(:title, label: "Title whatever", hint: "Make it quick") + + expect(page).to have_css ".help-text", text: "Make it quick" + expect(page).to have_css "input[aria-describedby='dummy_title-help-text']" + end + end end describe "#select" do