From e1a500d3199310b4835764b6c81260fd5a21f630 Mon Sep 17 00:00:00 2001 From: MadeTech Dushan Date: Mon, 7 Feb 2022 11:51:07 +0000 Subject: [PATCH] Refactor answer label Co-authored-by: baarkerlounger --- app/helpers/check_answers_helper.rb | 21 +++--------------- app/models/form/question.rb | 33 ++++++++++++++++++++++++++++- spec/fixtures/forms/2021_2022.json | 6 +++++- spec/models/form/question_spec.rb | 27 ++++++++++++++++++----- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index d15711cfd..e22b6ae24 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -12,25 +12,10 @@ module CheckAnswersHelper end def get_answer_label(question, case_log) - if question.id == "earnings" - linked_question = case_log.form.get_question("incfreq") - if question.answer_label(case_log).present? && linked_question.answer_label(case_log).present? - suffixes = { - "Weekly" => " every week", - "Monthly" => " every month", - "Yearly" => " every year", - } - [question.prefix, question.answer_label(case_log), suffixes[linked_question.answer_label(case_log)]].join("") - else - "You didn’t answer this question".html_safe - end + if question.answer_label.present? + question.answer_label else - answer = question.prefix == "£" ? ActionController::Base.helpers.number_to_currency(question.answer_label(case_log), delimiter: ",", format: "%n") : question.answer_label(case_log) - if answer.present? - [question.prefix, answer, question.suffix].join("") - else - "You didn’t answer this question".html_safe - end + "You didn’t answer this question".html_safe end end end diff --git a/app/models/form/question.rb b/app/models/form/question.rb index d6dd902ea..706afd6e7 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -38,7 +38,10 @@ class Form::Question return checkbox_answer_label(case_log) if type == "checkbox" return case_log[id]&.to_formatted_s(:govuk_date).to_s if type == "date" - return case_log[id].to_s if case_log[id].present? + answer = case_log[id].to_s if case_log[id].present? + answer_label = [prefix, format_value(answer), suffix_label(case_log)].join("") + + return answer_label if answer_label has_inferred_check_answers_value?(case_log) ? inferred_check_answers_value["value"] : "" end @@ -99,6 +102,28 @@ private answer.join(", ") end + def format_value(answer_label) + prefix == "£" ? ActionController::Base.helpers.number_to_currency(answer_label, delimiter: ",", format: "%n") : answer_label + end + + def suffix_label(case_log) + return "" unless suffix + return suffix if suffix.is_a?(String) + + label = "" + + suffix.each do |s| + condition = s["depends_on"] + next unless condition + + answer = case_log.send(condition.keys.first) + if answer == condition.values.first + label = ANSWER_SUFFIX_LABELS.has_key?(answer) ? ANSWER_SUFFIX_LABELS[answer] : answer + end + end + label + end + def conditional_on @conditional_on ||= form.conditional_question_conditions.select do |condition| condition[:to] == id @@ -121,4 +146,10 @@ private def enabled_inferred_answers(inferred_answers, case_log) inferred_answers.filter { |_key, value| value.all? { |condition_key, condition_value| case_log[condition_key] == condition_value } } end + + ANSWER_SUFFIX_LABELS = { + "Weekly" => " every week", + "Monthly" => " every month", + "Yearly" => " every year" + }.freeze end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 12fb93b95..3b9ca60da 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -358,7 +358,11 @@ "step": 1, "width": 5, "prefix": "£", - "suffix": "incfreq" + "suffix": [ + { "label": "every week", "depends_on" : { "incfreq": "Weekly"}}, + { "label": "every month", "depends_on" : { "incfreq": "Monthly"}}, + { "label": "every month", "depends_on" : { "incfreq": "Yearly"}} + ] }, "incfreq": { "check_answer_label": "Income Frequency", diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index f52d87b65..7fe6cff28 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -99,17 +99,17 @@ RSpec.describe Form::Question, type: :model do context "with a case log" do let(:case_log) { FactoryBot.build(:case_log, :in_progress) } + let(:question_id) { "incfreq" } it "has an answer label" do - case_log.earnings = 100 - expect(question.answer_label(case_log)).to eq("100") + case_log.incfreq = "Weekly" + expect(question.answer_label(case_log)).to eq("Weekly") end it "has an update answer link text helper" do - expect(question.update_answer_link_name(case_log)).to eq("Answer income") - case_log[question_id] = 5 + expect(question.update_answer_link_name(case_log)).to match(/Answer/) case_log["incfreq"] = "Weekly" - expect(question.update_answer_link_name(case_log)).to eq("Change income") + expect(question.update_answer_link_name(case_log)).to match(/Change/) end context "when type is date" do @@ -156,6 +156,23 @@ RSpec.describe Form::Question, type: :model do expect(question.enabled?(case_log)).to be true end end + + context "when answers have a suffix dependent on another answer" do + let(:section_id) { "rent_and_charges" } + let(:subsection_id) { "income_and_benefits" } + let(:page_id) { "net_income" } + let(:question_id) { "earnings" } + + it "displays the correct label for given suffix and answer the suffix depends on" do + case_log.incfreq = "Weekly" + case_log.earnings = 500 + expect(question.answer_label(case_log)).to eq("£500.00 every week") + case_log.incfreq = "Monthly" + expect(question.answer_label(case_log)).to eq("£500.00 every month") + case_log.incfreq = "Yearly" + expect(question.answer_label(case_log)).to eq("£500.00 every year") + end + end end describe ".completed?" do