From 4c6164e25edbea209abfc005a015393bae83fb07 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 24 Jan 2022 18:59:56 +0000 Subject: [PATCH] Refactor style logic into helper --- app/helpers/question_view_helper.rb | 6 +- 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 | 4 +- app/views/form/_select_question.html.erb | 2 +- app/views/form/_text_question.html.erb | 2 +- app/views/form/_textarea_question.html.erb | 2 +- app/views/form/page.html.erb | 2 +- spec/helpers/question_view_helper.rb | 75 ++++++++++++++++++++++ 10 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 spec/helpers/question_view_helper.rb diff --git a/app/helpers/question_view_helper.rb b/app/helpers/question_view_helper.rb index ecf29e574..5d85b2fa7 100644 --- a/app/helpers/question_view_helper.rb +++ b/app/helpers/question_view_helper.rb @@ -1,8 +1,8 @@ module QuestionViewHelper - def caption(caption, page_header, conditional) - return nil unless caption && page_header.blank? && !conditional + def caption(caption_text, page_header, conditional) + return nil unless caption_text && page_header.blank? && !conditional - { text: caption.html_safe, size: "l" } + { text: caption_text.html_safe, size: "l" } end def legend(question, page_header, conditional) diff --git a/app/views/form/_checkbox_question.html.erb b/app/views/form/_checkbox_question.html.erb index 97120a5cd..4ac23267b 100644 --- a/app/views/form/_checkbox_question.html.erb +++ b/app/views/form/_checkbox_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_check_boxes_fieldset question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe } do %> diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index d71db38e4..c453e1019 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_date_field question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: 20, diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index 8356a121f..606d2294c 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_number_field question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, min: question.min, max: question.max, step: question.step, diff --git a/app/views/form/_radio_question.html.erb b/app/views/form/_radio_question.html.erb index 83cf9f101..9a625ef76 100644 --- a/app/views/form/_radio_question.html.erb +++ b/app/views/form/_radio_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_radio_buttons_fieldset question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe } do %> @@ -16,7 +16,7 @@ <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %> <%= render partial: "#{conditional_question.type}_question", locals: { question: conditional_question, - caption: caption, + caption_text: caption_text, page_header: page_header, f: f, conditional: true diff --git a/app/views/form/_select_question.html.erb b/app/views/form/_select_question.html.erb index e02142869..ad986ac95 100644 --- a/app/views/form/_select_question.html.erb +++ b/app/views/form/_select_question.html.erb @@ -6,7 +6,7 @@ answers, :id, :name, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, options: { disabled: [""], selected: selected }, diff --git a/app/views/form/_text_question.html.erb b/app/views/form/_text_question.html.erb index 11bf9ad18..1fb67b84a 100644 --- a/app/views/form/_text_question.html.erb +++ b/app/views/form/_text_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_text_field question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: question.width ? question.width : nil, diff --git a/app/views/form/_textarea_question.html.erb b/app/views/form/_textarea_question.html.erb index 8ac2e44d7..59a8ca163 100644 --- a/app/views/form/_textarea_question.html.erb +++ b/app/views/form/_textarea_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_text_area question.id.to_sym, - caption: caption(caption, page_header, conditional), + caption: caption(caption_text, page_header, conditional), label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: question.width ? question.width : nil, diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index e2ec34dc8..3589a1c43 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -36,7 +36,7 @@ <% if question.read_only? %>
<% end %> - <%= render partial: "form/#{question.type}_question", locals: { question: question, caption: @subsection.label, page_header: @page.header, f: f, conditional: false } %> + <%= render partial: "form/#{question.type}_question", locals: { question: question, caption_text: @subsection.label, page_header: @page.header, f: f, conditional: false } %> <% end %> diff --git a/spec/helpers/question_view_helper.rb b/spec/helpers/question_view_helper.rb new file mode 100644 index 000000000..f34062a0d --- /dev/null +++ b/spec/helpers/question_view_helper.rb @@ -0,0 +1,75 @@ +require "rails_helper" + +RSpec.describe QuestionViewHelper do + let(:page_header) { "Some Page Header" } + let(:conditional) { false } + + describe "caption" do + let(:subject) { caption(caption_text, page_header, conditional) } + let(:caption_text) { "Some text" } + let(:caption_options_hash) { { text: caption_text.html_safe, size: "l" } } + + context "a page without a header" do + let(:page_header) { nil } + + it "returns an options hash" do + expect(subject).to eq(caption_options_hash) + end + end + + context "a page with a header" do + it "returns nil" do + expect(subject).to be_nil + end + end + + context "a conditional question" do + let(:conditional) { true } + it "returns nil" do + expect(subject).to be_nil + end + end + + context "a question without a caption" do + let(:caption_text) { nil } + + it "returns nil" do + expect(subject).to be_nil + end + end + end + + describe "legend" do + let(:question) { OpenStruct.new(header: "Some question header") } + let(:subject) { legend(question, page_header, conditional) } + let(:size) { "m" } + let(:tag) { "h2" } + let(:legend_options_hash) do + { text: "Some question header".html_safe, size: size, tag: tag } + end + + context "a page with a header" do + it "returns an options hash with a medium question header" do + expect(subject).to eq(legend_options_hash) + end + end + + context "a page without a header" do + let(:page_header) { nil } + let(:size) { "l" } + let(:tag) { "h1" } + + it "returns an options hash with a large question header" do + expect(subject).to eq(legend_options_hash) + end + end + + context "a conditional question" do + let(:conditional) { true } + + it "returns an options hash with a medium question header" do + expect(subject).to eq(legend_options_hash) + end + end + end +end