From dc0f9335073f551082d27675f5d6f73573d36295 Mon Sep 17 00:00:00 2001 From: Cameron Dutro Date: Tue, 15 Oct 2024 13:22:01 -0700 Subject: [PATCH] Fix caption templates --- app/forms/caption_template_form.rb | 6 ++++++ .../places_bellevue_caption.html.erb | 1 + .../places_lopez_caption.html.erb | 1 + .../places_seattle_caption.html.erb | 1 + app/lib/primer/forms/dsl/check_box_input.rb | 12 ++++-------- app/lib/primer/forms/dsl/input.rb | 10 +++++++++- app/lib/primer/forms/dsl/radio_button_input.rb | 4 ++++ test/lib/primer/forms_test.rb | 7 +++++-- 8 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 app/forms/caption_template_form/places_bellevue_caption.html.erb create mode 100644 app/forms/caption_template_form/places_lopez_caption.html.erb create mode 100644 app/forms/caption_template_form/places_seattle_caption.html.erb diff --git a/app/forms/caption_template_form.rb b/app/forms/caption_template_form.rb index 6bf4dce57e..d2a1cf700a 100644 --- a/app/forms/caption_template_form.rb +++ b/app/forms/caption_template_form.rb @@ -18,5 +18,11 @@ class CaptionTemplateForm < ApplicationForm age_radios.radio_button(value: "young", label: "10-15") age_radios.radio_button(value: "middle_aged", label: "16-21") end + + name_form.check_box_group(name: "places", label: "Cool places") do |check_group| + check_group.check_box(value: "lopez", label: "Lopez Island") + check_group.check_box(value: "bellevue", label: "Bellevue") + check_group.check_box(value: "seattle", label: "Seattle") + end end end diff --git a/app/forms/caption_template_form/places_bellevue_caption.html.erb b/app/forms/caption_template_form/places_bellevue_caption.html.erb new file mode 100644 index 0000000000..38396e2e58 --- /dev/null +++ b/app/forms/caption_template_form/places_bellevue_caption.html.erb @@ -0,0 +1 @@ +Bellevue caption diff --git a/app/forms/caption_template_form/places_lopez_caption.html.erb b/app/forms/caption_template_form/places_lopez_caption.html.erb new file mode 100644 index 0000000000..9d58f0eb8d --- /dev/null +++ b/app/forms/caption_template_form/places_lopez_caption.html.erb @@ -0,0 +1 @@ +Lopez caption diff --git a/app/forms/caption_template_form/places_seattle_caption.html.erb b/app/forms/caption_template_form/places_seattle_caption.html.erb new file mode 100644 index 0000000000..7be90fc00b --- /dev/null +++ b/app/forms/caption_template_form/places_seattle_caption.html.erb @@ -0,0 +1 @@ +Seattle caption diff --git a/app/lib/primer/forms/dsl/check_box_input.rb b/app/lib/primer/forms/dsl/check_box_input.rb index cbd10b5a42..93932c87a2 100644 --- a/app/lib/primer/forms/dsl/check_box_input.rb +++ b/app/lib/primer/forms/dsl/check_box_input.rb @@ -50,14 +50,10 @@ def supports_validation? false end - private - - def caption_template_name - @caption_template_name ||= if @scheme == :array - :"#{name}_#{value}" - else - name.to_sym - end + def values_disambiguate_template_names? + # Check boxes submitted as an array all have the same name, so we return true here + # to ensure different caption templates can be attached to individual check box inputs. + @scheme == :array end end end diff --git a/app/lib/primer/forms/dsl/input.rb b/app/lib/primer/forms/dsl/input.rb index e022525a86..14203a8364 100644 --- a/app/lib/primer/forms/dsl/input.rb +++ b/app/lib/primer/forms/dsl/input.rb @@ -300,6 +300,14 @@ def validation_error_icon_target "" end + # Whether or not the `value:` argument should be used to determine the caption template + # for a given field. This is useful in especially radio button groups where each option + # has the same name but a different value. Check box groups where the values are submitted + # as an array also use this feature, since each check box also has the same name. + def values_disambiguate_template_names? + false + end + private def input_data @@ -309,7 +317,7 @@ def input_data def caption_template_name return nil unless name - @caption_template_name ||= if respond_to?(:value) && value.present? + @caption_template_name ||= if values_disambiguate_template_names? && respond_to?(:value) && value.present? :"#{name}_#{value}" else name.to_sym diff --git a/app/lib/primer/forms/dsl/radio_button_input.rb b/app/lib/primer/forms/dsl/radio_button_input.rb index 8c1ead0ff1..86ded0cb14 100644 --- a/app/lib/primer/forms/dsl/radio_button_input.rb +++ b/app/lib/primer/forms/dsl/radio_button_input.rb @@ -42,6 +42,10 @@ def type def supports_validation? false end + + def values_disambiguate_template_names? + true + end end end end diff --git a/test/lib/primer/forms_test.rb b/test/lib/primer/forms_test.rb index d024523dad..3269d137ec 100644 --- a/test/lib/primer/forms_test.rb +++ b/test/lib/primer/forms_test.rb @@ -61,10 +61,13 @@ def test_renders_the_caption_template_when_present assert_selector ".FormControl-caption .color-fg-danger", text: "Check only if you are cool." assert_selector ".FormControl-caption .color-fg-danger", text: "A young thing." assert_selector ".FormControl-caption .color-fg-danger", text: "No longer a spring chicken." + assert_selector ".FormControl-caption", text: "Lopez caption" + assert_selector ".FormControl-caption", text: "Bellevue caption" + assert_selector ".FormControl-caption", text: "Seattle caption" end - def test_the_input_is_described_by_the_caption_when_caption_templates_are_used - num_inputs = 4 + def test_inputs_are_described_by_their_captions_when_caption_templates_are_used + num_inputs = 7 render_preview :caption_template_form caption_ids = page