From 1aa14fc9c082e27b2e44918168553b00c43d3998 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 7 Jan 2022 14:11:55 +0000 Subject: [PATCH] Render prefix and suffix --- app/models/case_log.rb | 10 +++---- app/models/form/question.rb | 16 +++++++++- app/views/form/_numeric_question.html.erb | 2 ++ config/forms/2021_2022.json | 18 +++++++----- spec/fixtures/forms/2021_2022.json | 4 ++- spec/views/form/page_view_spec.rb | 36 ++++++++++++++++++----- 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 61e60b984..95afa77b5 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -325,11 +325,11 @@ private dynamically_not_required << "tenancyother" end - if net_income_known == "Tenant prefers not to say" - dynamically_not_required << "earnings" - else - dynamically_not_required << "incref" - end + dynamically_not_required << if net_income_known == "Tenant prefers not to say" + "earnings" + else + "incref" + end start_range = (other_hhmemb || 0) + 2 (start_range..8).each do |n| diff --git a/app/models/form/question.rb b/app/models/form/question.rb index e4e843c0e..aa4cca5e0 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -3,7 +3,7 @@ class Form::Question :type, :min, :max, :step, :width, :fields_to_add, :result_field, :conditional_for, :readonly, :answer_options, :page, :check_answer_label, :inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value, - :guidance_partial + :guidance_partial, :prefix, :suffix def initialize(id, hsh, page) @id = id @@ -24,6 +24,8 @@ class Form::Question @inferred_answers = hsh["inferred_answers"] @inferred_check_answers_value = hsh["inferred_check_answers_value"] @hidden_in_check_answers = hsh["hidden_in_check_answers"] + @prefix = hsh["prefix"] + @suffix = hsh["suffix"] @page = page end @@ -80,6 +82,18 @@ class Form::Question case_log[id].present? end + def prefix_text(case_log = nil) + return prefix.to_s unless prefix && case_log.respond_to?(prefix) + + case_log.public_send(prefix).to_s + end + + def suffix_text(case_log = nil) + return suffix.to_s unless suffix && case_log.respond_to?(suffix) + + case_log.public_send(suffix).to_s + end + private def checkbox_answer_label(case_log) diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index 49872e8ee..fb8eb1126 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -6,5 +6,7 @@ hint: { text: question.hint_text&.html_safe }, min: question.min, max: question.max, step: question.step, width: question.width, :readonly => question.read_only?, + prefix_text: question.prefix_text(@case_log), + suffix_text: question.suffix_text(@case_log), **stimulus_html_attributes(question) %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 4479a06a3..1e02445f0 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1804,7 +1804,7 @@ "label": "Income and benefits", "depends_on": { "about_this_log": "completed" }, "pages": { - "net_income": { + "net_income_known": { "header": "Household’s combined income", "description": "", "questions": { @@ -1820,21 +1820,23 @@ "2": "Yes – the household has a yearly income", "divider_a": true, "3": "Tenant prefers not to say" - }, - "conditional_for": { - "earnings": ["Yes"], - "incfreq": ["Yes"] } - }, + } + } + }, + "net_income": { + "header": "", + "description": "", + "questions": { "earnings": { "check_answer_label": "Income", - "header": "What is the tenant’s /and partner’s combined income after tax?", + "header": "How much income does the household have in total every week?", "hint_text": "", "type": "numeric", "min": 0, "step": "1", "prefix": "£", - "suffix": "" + "suffix": "incfreq" } }, "soft_validations": { diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 645acdd1a..f07da547a 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -372,7 +372,9 @@ "type": "numeric", "min": 0, "step": 1, - "width": 5 + "width": 5, + "prefix": "£", + "suffix": "incfreq" }, "incfreq": { "check_answer_label": "Income Frequency", diff --git a/spec/views/form/page_view_spec.rb b/spec/views/form/page_view_spec.rb index 2666421b0..f8bc9789c 100644 --- a/spec/views/form/page_view_spec.rb +++ b/spec/views/form/page_view_spec.rb @@ -11,7 +11,7 @@ RSpec.describe "form/page" do let(:subsection) { form.get_subsection("income_and_benefits") } let(:page) { form.get_page("net_income") } let(:question) { page.questions.find { |q| q.id == "earnings" } } - let(:initial_attribs) { { type: "numeric", answer_options: nil } } + let(:initial_attribs) { { type: "numeric", answer_options: nil, prefix: nil, suffix: nil } } def assign_attributes(object, attrs) attrs.each_pair do |attr, value| @@ -19,10 +19,19 @@ RSpec.describe "form/page" do end end - context "given a question with extra guidance" do - let(:expected_guidance) { /What counts as income?/ } + after do + # Revert any changes we've made to avoid affecting other specs as the form, + # subsection, page, question objects being acted on are in memory + assign_attributes(question, initial_attribs) + end + + context "given a numeric question with prefix and suffix" do + let(:attribs) { { type: "numeric", prefix: "£", suffix: "incfreq" } } + let(:net_income_known) { "Yes – the household has a weekly income" } + let(:expected_suffix) { "Weekly" } before do + case_log.update!(net_income_known: net_income_known) assign(:case_log, case_log) assign(:page, page) assign(:subsection, subsection) @@ -30,12 +39,25 @@ RSpec.describe "form/page" do render end - after do - # Revert any changes we've made to avoid affecting other specs as the form, - # subsection, page, question objects being acted on are in memory - assign_attributes(question, initial_attribs) + it "renders prefix and suffix text" do + expect(rendered).to match(/govuk-input__prefix/) + expect(rendered).to match(/£/) + expect(rendered).to match(/govuk-input__suffix/) + expect(rendered).to match(expected_suffix) + end + end + + context "given a question with extra guidance" do + before do + assign(:case_log, case_log) + assign(:page, page) + assign(:subsection, subsection) + assign_attributes(question, attribs) + render end + let(:expected_guidance) { /What counts as income?/ } + context "with radio type" do let(:attribs) { { type: "radio", answer_options: { "1": "A", "2": "B" } } } it "renders the guidance partial for radio questions" do