diff --git a/lib/consul_form_builder.rb b/lib/consul_form_builder.rb index 778f0e8eb..27124ccf7 100644 --- a/lib/consul_form_builder.rb +++ b/lib/consul_form_builder.rb @@ -35,6 +35,13 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder super(attribute, tag_value, { label: default_label }.merge(options)) end + def select(attribute, choices, options = {}, html_options = {}) + label_with_hint(attribute, options.merge(label_options: label_options_for(options))) + + super(attribute, choices, options.merge(label: false, hint: nil), html_options.merge({ + aria: { describedby: help_text_id(attribute, options) } + })) + end + private def custom_label(attribute, text, options) diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index eed320cda..91d22ab89 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -3,7 +3,8 @@ require "rails_helper" describe ConsulFormBuilder do class DummyModel include ActiveModel::Model - attr_accessor :title + OPTIONS = %w[Good Bad Ugly].freeze + attr_accessor :title, :quality end let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) } @@ -30,6 +31,27 @@ describe ConsulFormBuilder do end end + describe "#select" do + it "renders the label and the select with the given options" do + render builder.select(:quality, DummyModel::OPTIONS) + + expect(page).to have_css "label", count: 1 + expect(page).to have_css "label", text: "Quality" + expect(page).to have_css "select", count: 1 + expect(page).to have_css "option", count: 3 + expect(page).to have_css "option", text: "Good" + expect(page).to have_css "option", text: "Bad" + expect(page).to have_css "option", text: "Ugly" + end + + it "accepts hints" do + render builder.select(:quality, DummyModel::OPTIONS, hint: "Ugly is neither good nor bad") + + expect(page).to have_css ".help-text", text: "Ugly is neither good nor bad" + expect(page).to have_css "select[aria-describedby='dummy_quality-help-text']" + end + end + attr_reader :content def render(content)