Browse Source

Refactor style logic into helper

pull/235/head
baarkerlounger 4 years ago
parent
commit
4c6164e25e
  1. 6
      app/helpers/question_view_helper.rb
  2. 2
      app/views/form/_checkbox_question.html.erb
  3. 2
      app/views/form/_date_question.html.erb
  4. 2
      app/views/form/_numeric_question.html.erb
  5. 4
      app/views/form/_radio_question.html.erb
  6. 2
      app/views/form/_select_question.html.erb
  7. 2
      app/views/form/_text_question.html.erb
  8. 2
      app/views/form/_textarea_question.html.erb
  9. 2
      app/views/form/page.html.erb
  10. 75
      spec/helpers/question_view_helper.rb

6
app/helpers/question_view_helper.rb

@ -1,8 +1,8 @@
module QuestionViewHelper module QuestionViewHelper
def caption(caption, page_header, conditional) def caption(caption_text, page_header, conditional)
return nil unless caption && page_header.blank? && !conditional return nil unless caption_text && page_header.blank? && !conditional
{ text: caption.html_safe, size: "l" } { text: caption_text.html_safe, size: "l" }
end end
def legend(question, page_header, conditional) def legend(question, page_header, conditional)

2
app/views/form/_checkbox_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_check_boxes_fieldset question.id.to_sym, <%= 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), legend: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe } do %> hint: { text: question.hint_text&.html_safe } do %>

2
app/views/form/_date_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_date_field question.id.to_sym, <%= 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), legend: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe }, hint: { text: question.hint_text&.html_safe },
width: 20, width: 20,

2
app/views/form/_numeric_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_number_field question.id.to_sym, <%= 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), label: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe }, hint: { text: question.hint_text&.html_safe },
min: question.min, max: question.max, step: question.step, min: question.min, max: question.max, step: question.step,

4
app/views/form/_radio_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_radio_buttons_fieldset question.id.to_sym, <%= 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), legend: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe } do %> 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 %> <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %>
<%= render partial: "#{conditional_question.type}_question", locals: { <%= render partial: "#{conditional_question.type}_question", locals: {
question: conditional_question, question: conditional_question,
caption: caption, caption_text: caption_text,
page_header: page_header, page_header: page_header,
f: f, f: f,
conditional: true conditional: true

2
app/views/form/_select_question.html.erb

@ -6,7 +6,7 @@
answers, answers,
:id, :id,
:name, :name,
caption: caption(caption, page_header, conditional), caption: caption(caption_text, page_header, conditional),
label: legend(question, page_header, conditional), label: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe }, hint: { text: question.hint_text&.html_safe },
options: { disabled: [""], selected: selected }, options: { disabled: [""], selected: selected },

2
app/views/form/_text_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_text_field question.id.to_sym, <%= 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), label: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe }, hint: { text: question.hint_text&.html_safe },
width: question.width ? question.width : nil, width: question.width ? question.width : nil,

2
app/views/form/_textarea_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<%= f.govuk_text_area question.id.to_sym, <%= 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), label: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe }, hint: { text: question.hint_text&.html_safe },
width: question.width ? question.width : nil, width: question.width ? question.width : nil,

2
app/views/form/page.html.erb

@ -36,7 +36,7 @@
<% if question.read_only? %> <% if question.read_only? %>
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m"> <hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m">
<% end %> <% 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 } %>
</div> </div>
<% end %> <% end %>

75
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
Loading…
Cancel
Save