Browse Source

Refactor answer label

Co-authored-by: baarkerlounger  <baarkerlounger@users.noreply.github.com>
pull/273/head
MadeTech Dushan 3 years ago
parent
commit
e1a500d319
  1. 19
      app/helpers/check_answers_helper.rb
  2. 33
      app/models/form/question.rb
  3. 6
      spec/fixtures/forms/2021_2022.json
  4. 27
      spec/models/form/question_spec.rb

19
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("")
if question.answer_label.present?
question.answer_label
else
"<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe
end
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
"<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe
end
end
end
end

33
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

6
spec/fixtures/forms/2021_2022.json vendored

@ -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",

27
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<span class=\"govuk-visually-hidden\"> income</span>")
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<span class=\"govuk-visually-hidden\"> income</span>")
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

Loading…
Cancel
Save