From 77face1e492affb725b26c1cf4f24b122f14f7f7 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 7 Oct 2021 14:38:36 +0100 Subject: [PATCH] Fix question counts for sections with conditional questions --- app/controllers/case_logs_controller.rb | 3 +- app/helpers/check_answers_helper.rb | 49 ++++++++++++++++------- app/models/form.rb | 9 +++-- app/views/form/check_answers.html.erb | 10 +++-- spec/helpers/check_answers_helper_spec.rb | 8 ++-- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 45bc05ee4..23bf8f514 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -40,8 +40,7 @@ class CaseLogsController < ApplicationController form = Form.new(2021, 2022) current_url = request.env["PATH_INFO"] subsection = current_url.split("/")[-2] - subsection_pages = form.pages_for_subsection(subsection) - render "form/check_answers", locals: { case_log: @case_log, subsection_pages: subsection_pages, subsection: subsection.humanize(capitalize: false) } + render "form/check_answers", locals: { case_log: @case_log, subsection: subsection } end form = Form.new(2021, 2022) diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index 4fd46ce9d..ab46812c7 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -1,16 +1,36 @@ module CheckAnswersHelper - def get_answered_questions_total(subsection_pages, case_log) - questions = subsection_pages.values.flat_map do |page| - page["questions"].keys + def total_answered_questions(subsection, case_log) + total_questions(subsection, case_log).keys.count do |question_key| + case_log[question_key].present? end - questions.count { |question| case_log[question].present? } end - def get_total_number_of_questions(subsection_pages) - questions = subsection_pages.values.flat_map do |page| - page["questions"].keys + def total_number_of_questions(subsection, case_log) + total_questions(subsection, case_log).count + end + + def total_questions(subsection, case_log) + form = Form.new(2021, 2022) + questions = form.questions_for_subsection(subsection) + question_keys = questions.keys + questions_not_applicable = [] + questions.reject do |question_key, question| + question.fetch("conditional_for", []).map do |conditional_question_key, condition| + if condition_not_met(case_log, question_key, condition) + questions_not_applicable << conditional_question_key + end + end + questions_not_applicable.include?(question_key) end - questions.count + end + + def condition_not_met(case_log, question_key, condition) + case_log[question_key].blank? || !(eval(case_log[question_key].to_s + condition)) + end + + def subsection_pages(subsection) + @form ||= Form.new(2021, 2022) + @subsection_pages ||= @form.pages_for_subsection(subsection) end def create_update_answer_link(case_log_answer, case_log_id, page) @@ -18,9 +38,9 @@ module CheckAnswersHelper link_to(link_name, "/case_logs/#{case_log_id}/#{page}", class: "govuk-link").html_safe end - def create_next_missing_question_link(case_log_id, subsection_pages, case_log) + def create_next_missing_question_link(case_log_id, subsection, case_log) pages_to_fill_in = [] - subsection_pages.each do |page_title, page_info| + subsection_pages(subsection).each do |page_title, page_info| page_info["questions"].any? { |question| case_log[question].blank? } pages_to_fill_in << page_title end @@ -28,12 +48,13 @@ module CheckAnswersHelper link_to("Answer the missing questions", url, class: "govuk-link").html_safe end - def display_answered_questions_summary(subsection_pages, case_log) - if get_answered_questions_total(subsection_pages, case_log) == get_total_number_of_questions(subsection_pages) + def display_answered_questions_summary(subsection, case_log) + # binding.pry + if total_answered_questions(subsection, case_log) == total_number_of_questions(subsection, case_log) '

You answered all the questions

'.html_safe else - "

You answered #{get_answered_questions_total(subsection_pages, case_log)} of #{get_total_number_of_questions(subsection_pages)} questions

- #{create_next_missing_question_link(case_log['id'], subsection_pages, case_log)}".html_safe + "

You answered #{total_answered_questions(subsection, case_log)} of #{total_number_of_questions(subsection, case_log)} questions

+ #{create_next_missing_question_link(case_log['id'], subsection, case_log)}".html_safe end end end diff --git a/app/models/form.rb b/app/models/form.rb index 9812d890f..031c0093c 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -37,6 +37,11 @@ class Form all_pages[page]["questions"] end + # Returns a hash with the questions as keys + def questions_for_subsection(subsection) + pages_for_subsection(subsection).map { |title, _value| questions_for_page(title) }.reduce(:merge) + end + def first_page_for_subsection(subsection) pages_for_subsection(subsection).keys.first end @@ -70,8 +75,4 @@ class Form pages_for_subsection(subsection).keys[current_page_idx - 1] end - - def questions_for_subsection(subsection) - pages_for_subsection(subsection).map { |title, _value| questions_for_page(title) }.reduce(:merge) - end end diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index a2102146d..b6533b4c2 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -1,11 +1,13 @@ <%= turbo_frame_tag "case_log_form", target: "_top" do %>
-

Check the answers you gave for <%= subsection %>

- <%= display_answered_questions_summary(subsection_pages, case_log) %> - <% subsection_pages.each do |page, page_info| %> +

Check the answers you gave for <%= subsection.humanize(capitalize: false) %>

+ <%= display_answered_questions_summary(subsection, case_log) %> + <% subsection_pages(subsection).each do |page, page_info| %> <% page_info["questions"].each do |question_title, question_info| %> - <%= render partial: 'form/check_answers_table', locals: { question_title: question_title, question_info: question_info, case_log: case_log, page: page } %> + <% if total_questions(subsection, case_log).include?(question_title) %> + <%= render partial: 'form/check_answers_table', locals: { question_title: question_title, question_info: question_info, case_log: case_log, page: page } %> + <%end %> <%end %> <% end %> <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index aabcc80cd..bd13ddf32 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -3,22 +3,22 @@ require "rails_helper" RSpec.describe CheckAnswersHelper do let(:case_log) { FactoryBot.create(:case_log) } let(:form) { Form.new(2021, 2022) } - let(:subsection_pages) { form.pages_for_subsection("income_and_benefits") } + let(:subsection) { "income_and_benefits" } describe "Get answered questions total" do it "returns 0 if no questions are answered" do - expect(get_answered_questions_total(subsection_pages, case_log)).to equal(0) + expect(total_answered_questions(subsection, case_log)).to equal(0) end it "returns 1 if 1 question gets answered" do case_log["net_income"] = "123" - expect(get_answered_questions_total(subsection_pages, case_log)).to equal(1) + expect(total_answered_questions(subsection, case_log)).to equal(1) end end describe "Get total number of questions" do it "returns the total number of questions for a subsection" do - expect(get_total_number_of_questions(subsection_pages)).to eq(4) + expect(total_number_of_questions(subsection, case_log)).to eq(4) end end end