From 31237afe9a7c29a0579f39607a918feb6f847404 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 12 Oct 2021 14:39:05 +0100 Subject: [PATCH] Pass in form to view from controller for check_answers --- app/controllers/case_logs_controller.rb | 3 ++- app/helpers/check_answers_helper.rb | 28 +++++++++-------------- app/models/form.rb | 4 +--- app/views/form/check_answers.html.erb | 6 ++--- spec/helpers/check_answers_helper_spec.rb | 21 ++++++++++------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 8e3bb441c..4e276aac7 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -49,10 +49,11 @@ class CaseLogsController < ApplicationController end def check_answers + form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) @case_log = CaseLog.find(params[:case_log_id]) current_url = request.env["PATH_INFO"] subsection = current_url.split("/")[-2] - render "form/check_answers", locals: { case_log: @case_log, subsection: subsection } + render "form/check_answers", locals: { case_log: @case_log, subsection: subsection, form: form } end form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index 31fa90be9..26cd34b0d 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -1,16 +1,15 @@ module CheckAnswersHelper - def total_answered_questions(subsection, case_log) - total_questions(subsection, case_log).keys.count do |question_key| + def total_answered_questions(subsection, case_log, form) + total_questions(subsection, case_log, form).keys.count do |question_key| case_log[question_key].present? end end - def total_number_of_questions(subsection, case_log) - total_questions(subsection, case_log).count + def total_number_of_questions(subsection, case_log, form) + total_questions(subsection, case_log, form).count end - def total_questions(subsection, case_log) - form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) #form should be passed in + def total_questions(subsection, case_log, form) questions = form.questions_for_subsection(subsection) questions_not_applicable = [] questions.reject do |question_key, question| @@ -36,19 +35,14 @@ module CheckAnswersHelper end end - def subsection_pages(subsection) - form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) #form should be passed in - form.pages_for_subsection(subsection) - end - def create_update_answer_link(case_log_answer, case_log_id, page) link_name = case_log_answer.blank? ? "Answer" : "Change" 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, case_log) + def create_next_missing_question_link(case_log_id, subsection, case_log, form) pages_to_fill_in = [] - subsection_pages(subsection).each do |page_title, page_info| + form.pages_for_subsection(subsection).each do |page_title, page_info| page_info["questions"].any? { |question| case_log[question].blank? } pages_to_fill_in << page_title end @@ -56,12 +50,12 @@ module CheckAnswersHelper link_to("Answer the missing questions", url, class: "govuk-link").html_safe end - def display_answered_questions_summary(subsection, case_log) - if total_answered_questions(subsection, case_log) == total_number_of_questions(subsection, case_log) + def display_answered_questions_summary(subsection, case_log, form) + if total_answered_questions(subsection, case_log, form) == total_number_of_questions(subsection, case_log, form) '

You answered all the questions

'.html_safe else - "

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 + "

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

+ #{create_next_missing_question_link(case_log['id'], subsection, case_log, form)}".html_safe end end end diff --git a/app/models/form.rb b/app/models/form.rb index 864ed24b8..031c0093c 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -2,10 +2,8 @@ class Form attr_reader :form_definition def initialize(start_year, end_year) - ENV["RAILS_ENV"] form_json = "config/forms/#{start_year}_#{end_year}.json" - # binding.pry - # raise "No form definition file exists for given year".freeze unless File.exist?(form_json) + raise "No form definition file exists for given year".freeze unless File.exist?(form_json) @form_definition = JSON.parse(File.open(form_json).read) end diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb index b6533b4c2..1ab567e44 100644 --- a/app/views/form/check_answers.html.erb +++ b/app/views/form/check_answers.html.erb @@ -2,10 +2,10 @@

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| %> + <%= display_answered_questions_summary(subsection, case_log, form) %> + <% form.pages_for_subsection(subsection).each do |page, page_info| %> <% page_info["questions"].each do |question_title, question_info| %> - <% if total_questions(subsection, case_log).include?(question_title) %> + <% if total_questions(subsection, case_log, form).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 %> diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 42cc71154..8660e81aa 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -16,32 +16,34 @@ RSpec.describe CheckAnswersHelper do let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } let(:subsection_with_radio_conditionals) { "household_needs" } + let(:form) { Form.new("test", "form") } describe "Get answered questions total" do it "returns 0 if no questions are answered" do - expect(total_answered_questions(subsection, case_log)).to equal(0) + expect(total_answered_questions(subsection, case_log, form)).to equal(0) end it "returns 1 if 1 question gets answered" do case_log["net_income"] = "123" - expect(total_answered_questions(subsection, case_log)).to equal(1) + expect(total_answered_questions(subsection, case_log, form)).to equal(1) end it "ignores questions with unmet numeric conditions" do case_log["tenant_code"] = "T1234" - expect(total_answered_questions(subsection_with_numeric_conditionals, case_log)).to equal(1) + expect(total_answered_questions(subsection_with_numeric_conditionals, case_log, form)).to equal(1) end it "includes conditional questions with met numeric conditions" do expect(total_answered_questions( subsection_with_numeric_conditionals, case_log_with_met_numeric_condition, + form, )).to equal(4) end it "ignores questions with unmet radio conditions" do case_log["armed_forces"] = "No" - expect(total_answered_questions(subsection_with_radio_conditionals, case_log)).to equal(1) + expect(total_answered_questions(subsection_with_radio_conditionals, case_log, form)).to equal(1) end it "includes conditional questions with met radio conditions" do @@ -50,34 +52,37 @@ RSpec.describe CheckAnswersHelper do expect(total_answered_questions( subsection_with_radio_conditionals, case_log_with_met_radio_condition, + form, )).to equal(3) end end describe "Get total number of questions" do it "returns the total number of questions for a subsection" do - expect(total_number_of_questions(subsection, case_log)).to eq(4) + expect(total_number_of_questions(subsection, case_log, form)).to eq(4) end it "ignores questions with unmet numeric conditions" do - expect(total_number_of_questions(subsection_with_numeric_conditionals, case_log)).to eq(7) + expect(total_number_of_questions(subsection_with_numeric_conditionals, case_log, form)).to eq(7) end it "includes conditional questions with met numeric conditions" do expect(total_number_of_questions( subsection_with_numeric_conditionals, case_log_with_met_numeric_condition, + form, )).to eq(15) end it "ignores questions with unmet radio conditions" do - expect(total_number_of_questions(subsection_with_radio_conditionals, case_log)).to eq(6) + expect(total_number_of_questions(subsection_with_radio_conditionals, case_log, form)).to eq(6) end it "includes conditional questions with met radio conditions" do expect(total_number_of_questions( subsection_with_radio_conditionals, case_log_with_met_radio_condition, + form, )).to eq(8) end @@ -99,7 +104,7 @@ RSpec.describe CheckAnswersHelper do it "raises an error" do allow_any_instance_of(Form).to receive(:questions_for_subsection).and_return(unimplemented_conditional) - expect { total_number_of_questions(subsection, case_log) }.to raise_error(RuntimeError, "Not implemented yet") + expect { total_number_of_questions(subsection, case_log, form) }.to raise_error(RuntimeError, "Not implemented yet") end end end