From 1f525c641371ba47dee2dfa81757b80b2e9578f1 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 7 Oct 2021 09:53:59 +0100 Subject: [PATCH] Refactor how we add stimulus controller html attributes so that we can combine them --- app/helpers/conditional_questions_helper.rb | 10 ----- app/helpers/numeric_questions_helper.rb | 12 ------ .../stimulus_controller_attribute_helper.rb | 39 +++++++++++++++++++ app/views/form/_checkbox_question.html.erb | 2 +- app/views/form/_date_question.html.erb | 2 +- app/views/form/_numeric_question.html.erb | 2 +- app/views/form/_radio_question.html.erb | 2 +- app/views/form/_text_question.html.erb | 2 +- ...mulus_controller_attribute_helper_spec.rb} | 6 +-- 9 files changed, 47 insertions(+), 30 deletions(-) delete mode 100644 app/helpers/numeric_questions_helper.rb create mode 100644 app/helpers/stimulus_controller_attribute_helper.rb rename spec/helpers/{numeric_questions_helper_spec.rb => stimulus_controller_attribute_helper_spec.rb} (75%) diff --git a/app/helpers/conditional_questions_helper.rb b/app/helpers/conditional_questions_helper.rb index 4713f7777..c77119243 100644 --- a/app/helpers/conditional_questions_helper.rb +++ b/app/helpers/conditional_questions_helper.rb @@ -8,14 +8,4 @@ module ConditionalQuestionsHelper def display_question_key_div(page_info, question_key) "style='display:none;'".html_safe if conditional_questions_for_page(page_info).include?(question_key) end - - def conditional_html_attributes(question) - return {} if question["conditional_for"].blank? - - { - "data-controller": "conditional-question", - "data-action": "conditional-question#displayConditional", - "data-info": question["conditional_for"].to_json, - } - end end diff --git a/app/helpers/numeric_questions_helper.rb b/app/helpers/numeric_questions_helper.rb deleted file mode 100644 index c0fe05ce3..000000000 --- a/app/helpers/numeric_questions_helper.rb +++ /dev/null @@ -1,12 +0,0 @@ -module NumericQuestionsHelper - def numeric_question_html_attributes(question) - return {} if question["fields-to-add"].blank? || question["result-field"].blank? - - { - "data-controller": "numeric-question", - "data-action": "numeric-question#calculateFields", - "data-target": "case-log-#{question['result-field'].to_s.dasherize}-field", - "data-calculated": question["fields-to-add"].to_json, - } - end -end diff --git a/app/helpers/stimulus_controller_attribute_helper.rb b/app/helpers/stimulus_controller_attribute_helper.rb new file mode 100644 index 000000000..825c0dde0 --- /dev/null +++ b/app/helpers/stimulus_controller_attribute_helper.rb @@ -0,0 +1,39 @@ +module StimulusControllerAttributeHelper + def stimulus_html_attributes(question) + attribs = [ + numeric_question_html_attributes(question), + conditional_html_attributes(question) + ] + merge_controller_attributes(*attribs) + end + + private + + def numeric_question_html_attributes(question) + return {} if question["fields-to-add"].blank? || question["result-field"].blank? + + { + "data-controller": "numeric-question", + "data-action": "numeric-question#calculateFields", + "data-target": "case-log-#{question['result-field'].to_s.dasherize}-field", + "data-calculated": question["fields-to-add"].to_json, + } + end + + def conditional_html_attributes(question) + return {} if question["conditional_for"].blank? + + { + "data-controller": "conditional-question", + "data-action": "conditional-question#displayConditional", + "data-info": question["conditional_for"].to_json, + } + end +end + +def merge_controller_attributes(*args) + args.flat_map(&:keys).uniq.each_with_object({}) do |key, hsh| + hsh[key] = args.map { |a| a.fetch(key, "") }.join(" ").strip + hsh + end +end diff --git a/app/views/form/_checkbox_question.html.erb b/app/views/form/_checkbox_question.html.erb index ef4029059..99bd3fd49 100644 --- a/app/views/form/_checkbox_question.html.erb +++ b/app/views/form/_checkbox_question.html.erb @@ -6,7 +6,7 @@ <% if key.starts_with?("divider") %> <%= f.govuk_check_box_divider %> <% else %> - <%= f.govuk_check_box question_key, val, label: { text: val }, **conditional_html_attributes(question) %> + <%= f.govuk_check_box question_key, val, label: { text: val }, **stimulus_html_attributes(question) %> <% end %> <% end %> <% end %> diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index 57fff1e32..417c7e163 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -2,5 +2,5 @@ hint: { text: question["hint_text"] }, legend: { text: question["header"].html_safe, size: "l"}, width: 20, - **conditional_html_attributes(question) + **stimulus_html_attributes(question) %> diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index 6de29a111..47909c0ba 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -3,5 +3,5 @@ label: { text: question["header"].html_safe, size: "l"}, min: question["min"], max: question["max"], step: question["step"], width: 20, :readonly => question["readonly"], - **numeric_question_html_attributes(question) + **stimulus_html_attributes(question) %> diff --git a/app/views/form/_radio_question.html.erb b/app/views/form/_radio_question.html.erb index f47f44854..3f653eb7d 100644 --- a/app/views/form/_radio_question.html.erb +++ b/app/views/form/_radio_question.html.erb @@ -7,7 +7,7 @@ <% if key.starts_with?("divider") %> <%= f.govuk_radio_divider %> <% else %> - <%= f.govuk_radio_button question_key, val, label: { text: val }, **conditional_html_attributes(question) %> + <%= f.govuk_radio_button question_key, val, label: { text: val }, **stimulus_html_attributes(question) %> <% end %> <% end %> <% end %> diff --git a/app/views/form/_text_question.html.erb b/app/views/form/_text_question.html.erb index 61c059fb9..4b45a8ceb 100644 --- a/app/views/form/_text_question.html.erb +++ b/app/views/form/_text_question.html.erb @@ -2,5 +2,5 @@ hint: { text: question["hint_text"] }, label: { text: question["header"].html_safe, size: "l"}, width: 20, - **conditional_html_attributes(question) + **stimulus_html_attributes(question) %> diff --git a/spec/helpers/numeric_questions_helper_spec.rb b/spec/helpers/stimulus_controller_attribute_helper_spec.rb similarity index 75% rename from spec/helpers/numeric_questions_helper_spec.rb rename to spec/helpers/stimulus_controller_attribute_helper_spec.rb index 5302d9eaa..d3ca1a5d4 100644 --- a/spec/helpers/numeric_questions_helper_spec.rb +++ b/spec/helpers/stimulus_controller_attribute_helper_spec.rb @@ -1,16 +1,16 @@ require "rails_helper" -RSpec.describe NumericQuestionsHelper do +RSpec.describe StimulusControllerAttributeHelper do let(:form) { Form.new(2021, 2022) } let(:questions) { form.questions_for_page("rent") } describe "html attributes" do it "returns empty hash if fields-to-add or result-field are empty " do - expect(numeric_question_html_attributes(questions["total_charge"])).to eq({}) + expect(stimulus_html_attributes(questions["total_charge"])).to eq({}) end it "returns html attributes if fields-to-add or result-field are not empty " do - expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({ + expect(stimulus_html_attributes(questions["basic_rent"])).to eq({ "data-controller": "numeric-question", "data-action": "numeric-question#calculateFields", "data-target": "case-log-#{questions['basic_rent']['result-field'].to_s.dasherize}-field",