diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 46cffbe13..27d3ede50 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -25,7 +25,7 @@ class FormController < ApplicationController if @case_log current_url = request.env["PATH_INFO"] subsection = @case_log.form.get_subsection(current_url.split("/")[-2]) - render "form/check_answers", locals: { subsection: } + render "form/check_answers", locals: { subsection:, current_user: } else render_not_found end diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index afa95069b..982cab7cc 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -1,9 +1,9 @@ module CheckAnswersHelper include GovukLinkHelper - def display_answered_questions_summary(subsection, case_log) - total = subsection.applicable_questions_count(case_log) - answered = subsection.answered_questions_count(case_log) + def display_answered_questions_summary(subsection, case_log, current_user) + total = total_count(subsection, case_log, current_user) + answered = answered_questions_count(subsection, case_log, current_user) if total == answered '
You answered all the questions.
'.html_safe else @@ -11,6 +11,24 @@ module CheckAnswersHelper end end +private + + def answered_questions_count(subsection, case_log, current_user) + answered_questions(subsection, case_log, current_user).count + end + + def answered_questions(subsection, case_log, current_user) + total_applicable_questions(subsection, case_log, current_user).select { |q| q.completed?(case_log) } + end + + def total_count(subsection, case_log, current_user) + total_applicable_questions(subsection, case_log, current_user).count + end + + def total_applicable_questions(subsection, case_log, current_user) + subsection.applicable_questions(case_log).reject { |q| q.hidden_in_check_answers?(case_log, current_user) } + end + def get_answer_label(question, case_log) question.answer_label(case_log).presence || "You didn’t answer this question".html_safe end diff --git a/app/models/form/question.rb b/app/models/form/question.rb index d4771052f..35d776e6c 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -66,7 +66,7 @@ class Form::Question conditional_on.all? { |condition| evaluate_condition(condition, case_log) } end - def hidden_in_check_answers?(case_log) + def hidden_in_check_answers?(case_log, _current_user = nil) if hidden_in_check_answers.is_a?(Hash) form.depends_on_met(hidden_in_check_answers["depends_on"], case_log) else diff --git a/app/models/form/setup/questions/created_by_id.rb b/app/models/form/setup/questions/created_by_id.rb index 6e303a24c..e01ae01c9 100644 --- a/app/models/form/setup/questions/created_by_id.rb +++ b/app/models/form/setup/questions/created_by_id.rb @@ -6,7 +6,6 @@ class Form::Setup::Questions::CreatedById < ::Form::Question @header = "Which user are you creating this log for?" @hint_text = "" @type = "select" - @derived = true @page = page end @@ -33,7 +32,11 @@ class Form::Setup::Questions::CreatedById < ::Form::Question answer_options[value] end - def hidden_in_check_answers - !form.current_user.support? + def hidden_in_check_answers?(_case_log, current_user) + !current_user.support? + end + + def derived? + true end end diff --git a/app/models/form/setup/questions/owning_organisation_id.rb b/app/models/form/setup/questions/owning_organisation_id.rb index c2e9b2703..ec48a4303 100644 --- a/app/models/form/setup/questions/owning_organisation_id.rb +++ b/app/models/form/setup/questions/owning_organisation_id.rb @@ -6,7 +6,6 @@ class Form::Setup::Questions::OwningOrganisationId < ::Form::Question @header = "Which organisation is the owning organisation for this log?" @hint_text = "" @type = "select" - @derived = true @page = page end @@ -33,7 +32,11 @@ class Form::Setup::Questions::OwningOrganisationId < ::Form::Question answer_options[value] end - def hidden_in_check_answers - !form.current_user.support? + def hidden_in_check_answers?(_case_log, current_user) + !current_user.support? + end + + def derived? + true end end diff --git a/app/models/form/setup/subsections/setup.rb b/app/models/form/setup/subsections/setup.rb index c99b9f125..92b4643b8 100644 --- a/app/models/form/setup/subsections/setup.rb +++ b/app/models/form/setup/subsections/setup.rb @@ -19,4 +19,25 @@ class Form::Subsections::Setup < ::Form::Subsection Form::Setup::Pages::PropertyReference.new(nil, nil, self), ] end + + def status(case_log) + unless enabled?(case_log) + return :cannot_start_yet + end + + qs = applicable_questions(case_log) + qs_optional_removed = qs.reject { |q| case_log.optional_fields.include?(q.id) } + return :not_started if qs.all? { |question| case_log[question.id].blank? || question.read_only? || question.derived? } + return :completed if qs_optional_removed.all? { |question| question.completed?(case_log) } + + :in_progress + end + + def applicable_questions(case_log) + questions.select do |q| + (q.displayed_to_user?(case_log) && !q.derived?) || + q.has_inferred_check_answers_value?(case_log) || + %w[owning_organisation_id created_by_id].include?(q.id) + end + end end diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index 558bf77c0..b6d361fb9 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -48,26 +48,9 @@ class Form::Subsection %i[in_progress completed].include?(status(case_log)) end - def applicable_questions_count(case_log) - applicable_questions(case_log).count - end - - def answered_questions_count(case_log) - answered_questions(case_log).count - end - def applicable_questions(case_log) questions.select do |q| - (q.displayed_to_user?(case_log) && !q.hidden_in_check_answers?(case_log) && !q.derived?) || - q.has_inferred_check_answers_value?(case_log) + (q.displayed_to_user?(case_log) && !q.derived?) || q.has_inferred_check_answers_value?(case_log) end end - - def answered_questions(case_log) - applicable_questions(case_log).select { |question| question.completed?(case_log) } - end - - def unanswered_questions(case_log) - applicable_questions(case_log) - answered_questions(case_log) - end end diff --git a/app/views/form/_check_answers_summary_list.html.erb b/app/views/form/_check_answers_summary_list.html.erb index a2aa3311f..6b7761437 100644 --- a/app/views/form/_check_answers_summary_list.html.erb +++ b/app/views/form/_check_answers_summary_list.html.erb @@ -1,5 +1,5 @@ <%= govuk_summary_list do |summary_list| %> - <% subsection.applicable_questions(@case_log).each do |question| %> + <% total_applicable_questions(subsection, @case_log, current_user).each do |question| %> <% summary_list.row do |row| %> <% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %> <% row.value do %> diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index 216bc747b..8db15d81e 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -15,7 +15,7 @@ <% if subsection.id == "setup" && subsection.status(@case_log) == :completed %> <%= govuk_inset_text(text: "Changing these answers might remove answers you’ve already given in other sections.") %> <% end %> - <%= display_answered_questions_summary(subsection, @case_log) %> + <%= display_answered_questions_summary(subsection, @case_log, current_user) %> <%= render partial: "form/check_answers_summary_list", locals: { subsection:, diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 6ec46628a..7e0bdec44 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -76,6 +76,7 @@ FactoryBot.define do tcharge { 325 } layear { 2 } waityear { 1 } + postcode_known { 1 } postcode_full { Faker::Address.postcode } reasonpref { 1 } cbl { 1 } diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 8c7b4e69d..593df3971 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -4,11 +4,12 @@ RSpec.describe CheckAnswersHelper do let(:form) { case_log.form } let(:subsection) { form.get_subsection("household_characteristics") } let(:case_log) { FactoryBot.build(:case_log, :in_progress) } + let(:current_user) { FactoryBot.build(:user) } describe "display_answered_questions_summary" do context "when a section hasn't been completed yet" do it "returns that you have unanswered questions" do - expect(display_answered_questions_summary(subsection, case_log)) + expect(display_answered_questions_summary(subsection, case_log, current_user)) .to match(/You have answered 2 of 7 questions./) end end @@ -20,9 +21,9 @@ RSpec.describe CheckAnswersHelper do case_log.propcode = "123" case_log.ecstat1 = 200 case_log.ecstat2 = 9 - expect(display_answered_questions_summary(subsection, case_log)) + expect(display_answered_questions_summary(subsection, case_log, current_user)) .to match(/You answered all the questions./) - expect(display_answered_questions_summary(subsection, case_log)) + expect(display_answered_questions_summary(subsection, case_log, current_user)) .not_to match(/href/) end end diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 4057e4687..78909766b 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -361,9 +361,9 @@ RSpec.describe Form::Question, type: :model do end it "can work out if the question will be shown in check answers" do - expect(question.hidden_in_check_answers?(case_log)).to be(false) + expect(question.hidden_in_check_answers?(case_log, nil)).to be(false) case_log.layear = 0 - expect(question.hidden_in_check_answers?(case_log)).to be(true) + expect(question.hidden_in_check_answers?(case_log, nil)).to be(true) end end end diff --git a/spec/models/form/setup/pages/created_by_spec.rb b/spec/models/form/setup/pages/created_by_spec.rb index dcf956d68..9e1c38dcb 100644 --- a/spec/models/form/setup/pages/created_by_spec.rb +++ b/spec/models/form/setup/pages/created_by_spec.rb @@ -36,11 +36,6 @@ RSpec.describe Form::Setup::Pages::CreatedBy, type: :model do context "when the current user is a support user" do let(:support_user) { FactoryBot.build(:user, :support) } - before do - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(support_user) - end - it "is shown" do expect(page.routed_to?(case_log, support_user)).to be true end @@ -49,11 +44,6 @@ RSpec.describe Form::Setup::Pages::CreatedBy, type: :model do context "when the current user is not a support user" do let(:user) { FactoryBot.build(:user) } - before do - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(user) - end - it "is not shown" do expect(page.routed_to?(case_log, user)).to be false end diff --git a/spec/models/form/setup/pages/organisation_spec.rb b/spec/models/form/setup/pages/organisation_spec.rb index 326cbe099..f4c7f3f23 100644 --- a/spec/models/form/setup/pages/organisation_spec.rb +++ b/spec/models/form/setup/pages/organisation_spec.rb @@ -36,11 +36,6 @@ RSpec.describe Form::Setup::Pages::Organisation, type: :model do context "when the current user is a support user" do let(:support_user) { FactoryBot.build(:user, :support) } - before do - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(support_user) - end - it "is shown" do expect(page.routed_to?(case_log, support_user)).to be true end @@ -49,11 +44,6 @@ RSpec.describe Form::Setup::Pages::Organisation, type: :model do context "when the current user is not a support user" do let(:user) { FactoryBot.build(:user) } - before do - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(user) - end - it "is not shown" do expect(page.routed_to?(case_log, user)).to be false end diff --git a/spec/models/form/setup/questions/created_by_id_spec.rb b/spec/models/form/setup/questions/created_by_id_spec.rb index 5f55110e4..84a7083be 100644 --- a/spec/models/form/setup/questions/created_by_id_spec.rb +++ b/spec/models/form/setup/questions/created_by_id_spec.rb @@ -53,28 +53,16 @@ RSpec.describe Form::Setup::Questions::CreatedById, type: :model do context "when the current user is support" do let(:support_user) { FactoryBot.build(:user, :support) } - before do - allow(page).to receive(:subsection).and_return(subsection) - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(support_user) - end - it "is shown in check answers" do - expect(question.hidden_in_check_answers).to be false + expect(question.hidden_in_check_answers?(nil, support_user)).to be false end end context "when the current user is not support" do let(:user) { FactoryBot.build(:user) } - before do - allow(page).to receive(:subsection).and_return(subsection) - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(user) - end - it "is not shown in check answers" do - expect(question.hidden_in_check_answers).to be true + expect(question.hidden_in_check_answers?(nil, user)).to be true end end diff --git a/spec/models/form/setup/questions/owning_organisation_id_spec.rb b/spec/models/form/setup/questions/owning_organisation_id_spec.rb index fc84cda33..5e4c5f787 100644 --- a/spec/models/form/setup/questions/owning_organisation_id_spec.rb +++ b/spec/models/form/setup/questions/owning_organisation_id_spec.rb @@ -53,28 +53,16 @@ RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do context "when the current user is support" do let(:support_user) { FactoryBot.build(:user, :support) } - before do - allow(page).to receive(:subsection).and_return(subsection) - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(support_user) - end - it "is shown in check answers" do - expect(question.hidden_in_check_answers).to be false + expect(question.hidden_in_check_answers?(nil, support_user)).to be false end end context "when the current user is not support" do let(:user) { FactoryBot.build(:user) } - before do - allow(page).to receive(:subsection).and_return(subsection) - allow(subsection).to receive(:form).and_return(form) - allow(form).to receive(:current_user).and_return(user) - end - it "is not shown in check answers" do - expect(question.hidden_in_check_answers).to be true + expect(question.hidden_in_check_answers?(nil, user)).to be true end end diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index 804292f6c..1b876520a 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -75,27 +75,6 @@ RSpec.describe Form::Subsection, type: :model do it "has question helpers for the number of applicable questions" do expected_questions = %w[tenancycode age1 sex1 ecstat1 hhmemb ecstat2 propcode] expect(subsection.applicable_questions(case_log).map(&:id)).to eq(expected_questions) - expect(subsection.applicable_questions_count(case_log)).to eq(7) - end - - it "has question helpers for the number of answered questions" do - subsection_definition = section_definition["subsections"]["household_needs"] - subsection = described_class.new("household_needs", subsection_definition, section) - expected_questions = %w[armedforces illness accessibility_requirements prevloc condition_effects] - case_log.armedforces = 3 - case_log.illness = 1 - case_log.housingneeds_a = 1 - case_log.previous_la_known = 1 - case_log.is_previous_la_inferred = false - case_log.prevloc = "E06000014" - case_log.illness_type_1 = 1 - expect(subsection.answered_questions(case_log).map(&:id)).to eq(expected_questions) - expect(subsection.answered_questions_count(case_log)).to eq(5) - end - - it "has a question helpers for the unanswered questions" do - expected_questions = %w[sex1 ecstat1 hhmemb ecstat2 propcode] - expect(subsection.unanswered_questions(case_log).map(&:id)).to eq(expected_questions) end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 6639a5a35..43a371c58 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -93,6 +93,7 @@ RSpec.describe Form, type: :model do end def answer_property_information(case_log) + case_log.postcode_known = 1 case_log.wchair = "No" end