From f49745d7bac07ab852a326e1f6ebfdf62e7ef3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Jun 2020 15:12:07 +0200 Subject: [PATCH 1/2] Simplify passing the object in the form builder The object is already a method, so we don't need to pass it around. --- lib/consul_form_builder.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index 3e60bfccf..7e4cffe2a 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -21,7 +21,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder def check_box(attribute, options = {}) if options[:label] != false - label = tag.span sanitize(label_text(object, attribute, options[:label])), class: "checkbox" + label = tag.span sanitize(label_text(attribute, options[:label])), class: "checkbox" super(attribute, options.merge(label: label, label_options: label_options_for(options))) else @@ -41,7 +41,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder if text == false super else - super(attribute, sanitize(label_text(object, attribute, text)), options) + super(attribute, sanitize(label_text(attribute, text)), options) end end @@ -50,7 +50,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder help_text(attribute, options) end - def label_text(object, attribute, text) + def label_text(attribute, text) if text.nil? || text == true default_label_text(object, attribute) else From c5f4cd6229813544311284d2e00de85af599546b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Jun 2020 17:23:30 +0200 Subject: [PATCH 2/2] Fix invalid "hint" attribute in forms Using `hint: false` was generating an input with `hint="false"` instead of generating no hint at all. --- lib/consul_form_builder.rb | 2 +- spec/lib/consul_form_builder_spec.rb | 42 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 spec/lib/consul_form_builder_spec.rb diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index 7e4cffe2a..066257d8f 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -13,7 +13,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder define_method field do |attribute, options = {}| label_with_hint(attribute, options.merge(label_options: label_options_for(options))) + super(attribute, options.merge( - label: false, hint: false, + label: false, hint: nil, aria: { describedby: help_text_id(attribute, options) } )) end diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb new file mode 100644 index 000000000..eed320cda --- /dev/null +++ b/spec/lib/consul_form_builder_spec.rb @@ -0,0 +1,42 @@ +require "rails_helper" + +describe ConsulFormBuilder do + class DummyModel + include ActiveModel::Model + attr_accessor :title + end + + let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) } + + describe "hints" do + it "does not generate hints by default" do + render builder.text_field(:title) + + expect(page).not_to have_css ".help-text" + expect(page).not_to have_css "input[aria-describedby]" + end + + it "generates text with a hint if provided" do + render builder.text_field(:title, 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 + + it "does not generate a hint attribute" do + render builder.text_field(:title) + + expect(page).not_to have_css "input[hint]" + end + end + + attr_reader :content + + def render(content) + @content = content + end + + def page + Capybara::Node::Simple.new(content) + end +end