Browse Source

Make it work for check answers

pull/686/head
baarkerlounger 3 years ago
parent
commit
5ccd68ad53
  1. 2
      app/controllers/form_controller.rb
  2. 24
      app/helpers/check_answers_helper.rb
  3. 2
      app/models/form/question.rb
  4. 9
      app/models/form/setup/questions/created_by_id.rb
  5. 9
      app/models/form/setup/questions/owning_organisation_id.rb
  6. 21
      app/models/form/setup/subsections/setup.rb
  7. 19
      app/models/form/subsection.rb
  8. 2
      app/views/form/_check_answers_summary_list.html.erb
  9. 2
      app/views/form/check_answers.html.erb
  10. 1
      spec/factories/case_log.rb
  11. 7
      spec/helpers/check_answers_helper_spec.rb
  12. 4
      spec/models/form/question_spec.rb
  13. 10
      spec/models/form/setup/pages/created_by_spec.rb
  14. 10
      spec/models/form/setup/pages/organisation_spec.rb
  15. 16
      spec/models/form/setup/questions/created_by_id_spec.rb
  16. 16
      spec/models/form/setup/questions/owning_organisation_id_spec.rb
  17. 21
      spec/models/form/subsection_spec.rb
  18. 1
      spec/models/form_spec.rb

2
app/controllers/form_controller.rb

@ -25,7 +25,7 @@ class FormController < ApplicationController
if @case_log if @case_log
current_url = request.env["PATH_INFO"] current_url = request.env["PATH_INFO"]
subsection = @case_log.form.get_subsection(current_url.split("/")[-2]) 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 else
render_not_found render_not_found
end end

24
app/helpers/check_answers_helper.rb

@ -1,9 +1,9 @@
module CheckAnswersHelper module CheckAnswersHelper
include GovukLinkHelper include GovukLinkHelper
def display_answered_questions_summary(subsection, case_log) def display_answered_questions_summary(subsection, case_log, current_user)
total = subsection.applicable_questions_count(case_log) total = total_count(subsection, case_log, current_user)
answered = subsection.answered_questions_count(case_log) answered = answered_questions_count(subsection, case_log, current_user)
if total == answered if total == answered
'<p class="govuk-body">You answered all the questions.</p>'.html_safe '<p class="govuk-body">You answered all the questions.</p>'.html_safe
else else
@ -11,6 +11,24 @@ module CheckAnswersHelper
end end
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) def get_answer_label(question, case_log)
question.answer_label(case_log).presence || "<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe question.answer_label(case_log).presence || "<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe
end end

2
app/models/form/question.rb

@ -66,7 +66,7 @@ class Form::Question
conditional_on.all? { |condition| evaluate_condition(condition, case_log) } conditional_on.all? { |condition| evaluate_condition(condition, case_log) }
end 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) if hidden_in_check_answers.is_a?(Hash)
form.depends_on_met(hidden_in_check_answers["depends_on"], case_log) form.depends_on_met(hidden_in_check_answers["depends_on"], case_log)
else else

9
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?" @header = "Which user are you creating this log for?"
@hint_text = "" @hint_text = ""
@type = "select" @type = "select"
@derived = true
@page = page @page = page
end end
@ -33,7 +32,11 @@ class Form::Setup::Questions::CreatedById < ::Form::Question
answer_options[value] answer_options[value]
end end
def hidden_in_check_answers def hidden_in_check_answers?(_case_log, current_user)
!form.current_user.support? !current_user.support?
end
def derived?
true
end end
end end

9
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?" @header = "Which organisation is the owning organisation for this log?"
@hint_text = "" @hint_text = ""
@type = "select" @type = "select"
@derived = true
@page = page @page = page
end end
@ -33,7 +32,11 @@ class Form::Setup::Questions::OwningOrganisationId < ::Form::Question
answer_options[value] answer_options[value]
end end
def hidden_in_check_answers def hidden_in_check_answers?(_case_log, current_user)
!form.current_user.support? !current_user.support?
end
def derived?
true
end end
end end

21
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), Form::Setup::Pages::PropertyReference.new(nil, nil, self),
] ]
end 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 end

19
app/models/form/subsection.rb

@ -48,26 +48,9 @@ class Form::Subsection
%i[in_progress completed].include?(status(case_log)) %i[in_progress completed].include?(status(case_log))
end 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) def applicable_questions(case_log)
questions.select do |q| questions.select do |q|
(q.displayed_to_user?(case_log) && !q.hidden_in_check_answers?(case_log) && !q.derived?) || (q.displayed_to_user?(case_log) && !q.derived?) || q.has_inferred_check_answers_value?(case_log)
q.has_inferred_check_answers_value?(case_log)
end end
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 end

2
app/views/form/_check_answers_summary_list.html.erb

@ -1,5 +1,5 @@
<%= govuk_summary_list do |summary_list| %> <%= 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| %> <% summary_list.row do |row| %>
<% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %> <% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %>
<% row.value do %> <% row.value do %>

2
app/views/form/check_answers.html.erb

@ -15,7 +15,7 @@
<% if subsection.id == "setup" && subsection.status(@case_log) == :completed %> <% 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.") %> <%= govuk_inset_text(text: "Changing these answers might remove answers you’ve already given in other sections.") %>
<% end %> <% 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: { <%= render partial: "form/check_answers_summary_list", locals: {
subsection:, subsection:,

1
spec/factories/case_log.rb

@ -76,6 +76,7 @@ FactoryBot.define do
tcharge { 325 } tcharge { 325 }
layear { 2 } layear { 2 }
waityear { 1 } waityear { 1 }
postcode_known { 1 }
postcode_full { Faker::Address.postcode } postcode_full { Faker::Address.postcode }
reasonpref { 1 } reasonpref { 1 }
cbl { 1 } cbl { 1 }

7
spec/helpers/check_answers_helper_spec.rb

@ -4,11 +4,12 @@ RSpec.describe CheckAnswersHelper do
let(:form) { case_log.form } let(:form) { case_log.form }
let(:subsection) { form.get_subsection("household_characteristics") } let(:subsection) { form.get_subsection("household_characteristics") }
let(:case_log) { FactoryBot.build(:case_log, :in_progress) } let(:case_log) { FactoryBot.build(:case_log, :in_progress) }
let(:current_user) { FactoryBot.build(:user) }
describe "display_answered_questions_summary" do describe "display_answered_questions_summary" do
context "when a section hasn't been completed yet" do context "when a section hasn't been completed yet" do
it "returns that you have unanswered questions" 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./) .to match(/You have answered 2 of 7 questions./)
end end
end end
@ -20,9 +21,9 @@ RSpec.describe CheckAnswersHelper do
case_log.propcode = "123" case_log.propcode = "123"
case_log.ecstat1 = 200 case_log.ecstat1 = 200
case_log.ecstat2 = 9 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./) .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/) .not_to match(/href/)
end end
end end

4
spec/models/form/question_spec.rb

@ -361,9 +361,9 @@ RSpec.describe Form::Question, type: :model do
end end
it "can work out if the question will be shown in check answers" do 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 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 end
end end

10
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 context "when the current user is a support user" do
let(:support_user) { FactoryBot.build(:user, :support) } 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 it "is shown" do
expect(page.routed_to?(case_log, support_user)).to be true expect(page.routed_to?(case_log, support_user)).to be true
end 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 context "when the current user is not a support user" do
let(:user) { FactoryBot.build(:user) } 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 it "is not shown" do
expect(page.routed_to?(case_log, user)).to be false expect(page.routed_to?(case_log, user)).to be false
end end

10
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 context "when the current user is a support user" do
let(:support_user) { FactoryBot.build(:user, :support) } 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 it "is shown" do
expect(page.routed_to?(case_log, support_user)).to be true expect(page.routed_to?(case_log, support_user)).to be true
end 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 context "when the current user is not a support user" do
let(:user) { FactoryBot.build(:user) } 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 it "is not shown" do
expect(page.routed_to?(case_log, user)).to be false expect(page.routed_to?(case_log, user)).to be false
end end

16
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 context "when the current user is support" do
let(:support_user) { FactoryBot.build(:user, :support) } 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 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
end end
context "when the current user is not support" do context "when the current user is not support" do
let(:user) { FactoryBot.build(:user) } 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 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
end end

16
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 context "when the current user is support" do
let(:support_user) { FactoryBot.build(:user, :support) } 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 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
end end
context "when the current user is not support" do context "when the current user is not support" do
let(:user) { FactoryBot.build(:user) } 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 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
end end

21
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 it "has question helpers for the number of applicable questions" do
expected_questions = %w[tenancycode age1 sex1 ecstat1 hhmemb ecstat2 propcode] 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(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
end end

1
spec/models/form_spec.rb

@ -93,6 +93,7 @@ RSpec.describe Form, type: :model do
end end
def answer_property_information(case_log) def answer_property_information(case_log)
case_log.postcode_known = 1
case_log.wchair = "No" case_log.wchair = "No"
end end

Loading…
Cancel
Save