diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index b88425b54..e6a3a3ecd 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -50,7 +50,6 @@ class CaseLogsController < ApplicationController end def edit - @form = FormHandler.instance.get_form("2021_2022") @case_log = current_user.case_logs.find_by(id: params[:id]) if @case_log render :edit diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 5665731c7..5c1c23c5b 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -4,16 +4,15 @@ class FormController < ApplicationController before_action :find_resource_by_named_id, except: [:submit_form] def submit_form - form = FormHandler.instance.get_form("2021_2022") if @case_log - page = form.get_page(params[:case_log][:page]) + page = @case_log.form.get_page(params[:case_log][:page]) responses_for_page = responses_for_page(page) if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors? - redirect_path = form.next_page_redirect_path(page, @case_log) + redirect_path = @case_log.form.next_page_redirect_path(page, @case_log) redirect_to(send(redirect_path, @case_log)) else - subsection = form.subsection_for_page(page) - render "form/page", locals: { form: form, page: page, subsection: subsection.label }, status: :unprocessable_entity + subsection = @case_log.form.subsection_for_page(page) + render "form/page", locals: { page: page, subsection: subsection.label }, status: :unprocessable_entity end else render_not_found @@ -21,24 +20,25 @@ class FormController < ApplicationController end def check_answers - form = FormHandler.instance.get_form("2021_2022") if @case_log current_url = request.env["PATH_INFO"] - subsection = form.get_subsection(current_url.split("/")[-2]) - render "form/check_answers", locals: { subsection: subsection, form: form } + subsection = @case_log.form.get_subsection(current_url.split("/")[-2]) + render "form/check_answers", locals: { subsection: subsection } else render_not_found end end - form = FormHandler.instance.get_form("2021_2022") - form.pages.map do |page| - define_method(page.id) do |_errors = {}| - if @case_log - subsection = form.subsection_for_page(page) - render "form/page", locals: { form: form, page: page, subsection: subsection.label } - else - render_not_found + FormHandler.instance.forms.each do |_key, form| + form.pages.map do |page| + define_method(page.id) do |_errors = {}| + if @case_log + subsection = @case_log.form.subsection_for_page(page) + case_log_form_page = @case_log.form.get_page(page.id) + render "form/page", locals: { page: case_log_form_page, subsection: subsection.label } + else + render_not_found + end end end end diff --git a/app/controllers/soft_validations_controller.rb b/app/controllers/soft_validations_controller.rb index 0f0baf376..c294171ce 100644 --- a/app/controllers/soft_validations_controller.rb +++ b/app/controllers/soft_validations_controller.rb @@ -4,8 +4,7 @@ class SoftValidationsController < ApplicationController def show @case_log = CaseLog.find(params[:case_log_id]) page_id = request.env["PATH_INFO"].split("/")[-2] - form = FormHandler.instance.get_form("2021_2022") - page = form.get_page(page_id) + page = @case_log.form.get_page(page_id) if page_requires_soft_validation_override?(page) errors = @case_log.soft_errors.values.first render json: { show: true, label: errors.message, hint: errors.hint_text } diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 96df32180..a6935f620 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -15,14 +15,14 @@ module TasklistHelper in_progress: "govuk-tag--blue", }.freeze - def get_next_incomplete_section(form, case_log) - form.subsections.find { |subsection| subsection.is_incomplete?(case_log) } + def get_next_incomplete_section(case_log) + case_log.form.subsections.find { |subsection| subsection.is_incomplete?(case_log) } end - def get_subsections_count(form, case_log, status = :all) - return form.subsections.count if status == :all + def get_subsections_count(case_log, status = :all) + return case_log.form.subsections.count if status == :all - form.subsections.count { |subsection| subsection.status(case_log) == status } + case_log.form.subsections.count { |subsection| subsection.status(case_log) == status } end def first_page_or_check_answers(subsection, case_log) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a526c1c79..b65b7ef44 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -136,6 +136,15 @@ class CaseLog < ApplicationRecord la_known first_time_property_let_as_social_housing].freeze + def form + FormHandler.instance.get_form(form_name) + end + + def form_name + start_year = year || 2021 + "#{start_year}_#{start_year + 1}" + end + def self.editable_fields attribute_names - AUTOGENERATED_FIELDS end diff --git a/app/models/form.rb b/app/models/form.rb index 1c24f71b3..370a3cd41 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -1,10 +1,15 @@ class Form - attr_reader :form_definition, :sections, :subsections, :pages, :questions + attr_reader :form_definition, :sections, :subsections, :pages, :questions, + :start_year, :end_year, :type, :name - def initialize(form_path) + def initialize(form_path, name) raise "No form definition file exists for given year".freeze unless File.exist?(form_path) @form_definition = JSON.parse(File.open(form_path).read) + @name = name + @start_year = form_definition["start_year"] + @end_year = form_definition["end_year"] + @type = form_definition["form_type"] @sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } @subsections = sections.flat_map(&:subsections) @pages = subsections.flat_map(&:pages) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 4e8bee80b..bfceff1a9 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -7,8 +7,6 @@ class FormHandler end def get_form(form) - return @forms["test_form"] ||= Form.new("test_form") if ENV["RAILS_ENV"] == "test" - @forms[form] ||= Form.new(form) end @@ -16,13 +14,16 @@ private def get_all_forms forms = {} - directories = ["config/forms", "spec/fixtures/forms"] directories.each do |directory| Dir.glob("#{directory}/*.json").each do |form_path| form_name = form_path.sub(".json", "").split("/")[-1] - forms[form_name] = Form.new(form_path) + forms[form_name] = Form.new(form_path, form_name) end end forms end + + def directories + Rails.env.test? ? ["spec/fixtures/forms"] : ["config/forms"] + end end diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index 87eb00a7b..6765d6f82 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -1,5 +1,5 @@
    - <% @form.sections.map do |section| %> + <% @case_log.form.sections.map do |section| %>
  1. diff --git a/app/views/case_logs/edit.html.erb b/app/views/case_logs/edit.html.erb index bb83d8370..ab0466125 100644 --- a/app/views/case_logs/edit.html.erb +++ b/app/views/case_logs/edit.html.erb @@ -12,9 +12,9 @@ <%= @case_log.status.to_s.humanize.downcase %>

    -

    You’ve completed <%= get_subsections_count(@form, @case_log, :completed) %> of <%= get_subsections_count(@form, @case_log, :all) %> sections.

    +

    You’ve completed <%= get_subsections_count(@case_log, :completed) %> of <%= get_subsections_count(@case_log, :all) %> sections.

    - <% next_incomplete_section = get_next_incomplete_section(@form, @case_log).id %> + <% next_incomplete_section = get_next_incomplete_section(@case_log).id %>

    - <%= render "tasklist", locals: { form: @form } %> + <%= render "tasklist" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 80dfa5352..8f7a1db47 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,9 +28,6 @@ Rails.application.routes.draw do end end - form_handler = FormHandler.instance - form = form_handler.get_form("2021_2022") - resources :case_logs, path: "/logs" do collection do post "bulk-upload", to: "bulk_upload#bulk_upload" @@ -41,13 +38,15 @@ Rails.application.routes.draw do post "form", to: "form#submit_form" end - form.pages.map do |page| - get page.id.to_s.dasherize, to: "form##{page.id}" - get "#{page.id.to_s.dasherize}/soft-validations", to: "soft_validations#show" if page.has_soft_validations? - end + FormHandler.instance.forms.each do |_key, form| + form.pages.map do |page| + get page.id.to_s.dasherize, to: "form##{page.id}" + get "#{page.id.to_s.dasherize}/soft-validations", to: "soft_validations#show" if page.has_soft_validations? + end - form.subsections.map do |subsection| - get "#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers" + form.subsections.map do |subsection| + get "#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers" + end end end diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index b459e38fc..6bb24ab91 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -136,7 +136,7 @@ FactoryBot.define do startdate { Time.zone.now } day { Time.zone.now.day } month { Time.zone.now.month } - year { Time.zone.now.year } + year { 2021 } armedforces { 1 } builtype { 1 } unitletas { 2 } diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/2021_2022.json similarity index 100% rename from spec/fixtures/forms/test_form.json rename to spec/fixtures/forms/2021_2022.json diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 4a90a53ea..ee6612a49 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" RSpec.describe CheckAnswersHelper do - form_handler = FormHandler.instance - let(:form) { form_handler.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:subsection) { form.get_subsection("household_characteristics") } let(:case_log) { FactoryBot.build(:case_log, :in_progress) } diff --git a/spec/helpers/conditional_questions_helper_spec.rb b/spec/helpers/conditional_questions_helper_spec.rb index 2b86cd7f2..84f078411 100644 --- a/spec/helpers/conditional_questions_helper_spec.rb +++ b/spec/helpers/conditional_questions_helper_spec.rb @@ -1,9 +1,8 @@ require "rails_helper" RSpec.describe ConditionalQuestionsHelper do - form_handler = FormHandler.instance - let(:form) { form_handler.get_form("test_form") } - let(:page) { form.get_page("armed_forces") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:page) { case_log.form.get_page("armed_forces") } describe "conditional questions for page" do let(:conditional_pages) { %w[leftreg reservist] } diff --git a/spec/helpers/question_attribute_helper_spec.rb b/spec/helpers/question_attribute_helper_spec.rb index a3b784bc0..88c30d3e9 100644 --- a/spec/helpers/question_attribute_helper_spec.rb +++ b/spec/helpers/question_attribute_helper_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" RSpec.describe QuestionAttributeHelper do - form_handler = FormHandler.instance - let(:form) { form_handler.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:questions) { form.get_page("rent").questions } describe "html attributes" do diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 704866ce9..d55cd8258 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -7,44 +7,42 @@ RSpec.describe TasklistHelper do end let(:empty_case_log) { FactoryBot.create(:case_log) } let(:case_log) { FactoryBot.create(:case_log, :in_progress) } - form_handler = FormHandler.instance - let(:form) { form_handler.get_form("test_form") } describe "get next incomplete section" do it "returns the first subsection name if it is not completed" do - expect(get_next_incomplete_section(form, case_log).id).to eq("household_characteristics") + expect(get_next_incomplete_section(case_log).id).to eq("household_characteristics") end it "returns the first subsection name if it is partially completed" do case_log["tenant_code"] = 123 - expect(get_next_incomplete_section(form, case_log).id).to eq("household_characteristics") + expect(get_next_incomplete_section(case_log).id).to eq("household_characteristics") end end describe "get sections count" do it "returns the total of sections if no status is given" do - expect(get_subsections_count(form, empty_case_log)).to eq(9) + expect(get_subsections_count(empty_case_log)).to eq(9) end it "returns 0 sections for completed sections if no sections are completed" do - expect(get_subsections_count(form, empty_case_log, :completed)).to eq(0) + expect(get_subsections_count(empty_case_log, :completed)).to eq(0) end it "returns the number of not started sections" do - expect(get_subsections_count(form, empty_case_log, :not_started)).to eq(8) + expect(get_subsections_count(empty_case_log, :not_started)).to eq(8) end it "returns the number of sections in progress" do - expect(get_subsections_count(form, case_log, :in_progress)).to eq(3) + expect(get_subsections_count(case_log, :in_progress)).to eq(3) end it "returns 0 for invalid state" do - expect(get_subsections_count(form, case_log, :fake)).to eq(0) + expect(get_subsections_count(case_log, :fake)).to eq(0) end end describe "get_first_page_or_check_answers" do - let(:subsection) { form.get_subsection("household_characteristics") } + let(:subsection) { case_log.form.get_subsection("household_characteristics") } it "returns the check answers page path if the section has been started already" do expect(first_page_or_check_answers(subsection, case_log)).to match(/check-answers/) @@ -56,7 +54,7 @@ RSpec.describe TasklistHelper do end describe "subsection link" do - let(:subsection) { form.get_subsection("household_characteristics") } + let(:subsection) { case_log.form.get_subsection("household_characteristics") } context "for a subsection that's enabled" do it "returns the subsection link url" do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 5326b73c0..ae3d82b80 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -8,6 +8,16 @@ RSpec.describe Form, type: :model do RequestHelper.stub_http_requests end + describe "#form" do + let(:case_log) { FactoryBot.build(:case_log) } + let(:case_log_year_2) { FactoryBot.build(:case_log, year: 2023) } + it "has returns the correct form based on the start date" do + expect(case_log.form_name).to eq("2021_2022") + expect(case_log_year_2.form_name).to eq("2023_2024") + expect(case_log.form).to be_a(Form) + end + end + describe "#new" do it "validates age is a number" do expect { diff --git a/spec/models/form/page_spec.rb b/spec/models/form/page_spec.rb index 0da9d7b19..a0eac90e9 100644 --- a/spec/models/form/page_spec.rb +++ b/spec/models/form/page_spec.rb @@ -1,7 +1,8 @@ require "rails_helper" RSpec.describe Form::Page, type: :model do - let(:form) { FormHandler.instance.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:section_id) { "rent_and_charges" } let(:section_definition) { form.form_definition["sections"][section_id] } let(:section) { Form::Section.new(section_id, section_definition, form) } diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index aff54e543..619cd7276 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -1,7 +1,8 @@ require "rails_helper" RSpec.describe Form::Question, type: :model do - let(:form) { FormHandler.instance.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:section_id) { "rent_and_charges" } let(:section_definition) { form.form_definition["sections"][section_id] } let(:section) { Form::Section.new(section_id, section_definition, form) } diff --git a/spec/models/form/section_spec.rb b/spec/models/form/section_spec.rb index cecf2c3a7..da63c68e2 100644 --- a/spec/models/form/section_spec.rb +++ b/spec/models/form/section_spec.rb @@ -1,7 +1,8 @@ require "rails_helper" RSpec.describe Form::Section, type: :model do - let(:form) { FormHandler.instance.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:section_id) { "household" } let(:section_definition) { form.form_definition["sections"][section_id] } subject { Form::Section.new(section_id, section_definition, form) } diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index 0bce0961c..f64adc73e 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -1,7 +1,8 @@ require "rails_helper" RSpec.describe Form::Subsection, type: :model do - let(:form) { FormHandler.instance.get_form("test_form") } + let(:case_log) { FactoryBot.build(:case_log) } + let(:form) { case_log.form } let(:section_id) { "household" } let(:section_definition) { form.form_definition["sections"][section_id] } let(:section) { Form::Section.new(section_id, section_definition, form) } diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 4e5883ec2..188ca33d1 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -1,19 +1,20 @@ require "rails_helper" RSpec.describe FormHandler do + let(:test_form_name) { "2021_2022" } describe "Get all forms" do it "should be able to load all the forms" do form_handler = FormHandler.instance all_forms = form_handler.forms expect(all_forms.count).to be >= 1 - expect(all_forms["test_form"]).to be_a(Form) + expect(all_forms[test_form_name]).to be_a(Form) end end describe "Get specific form" do it "should be able to load a specific form" do form_handler = FormHandler.instance - form = form_handler.get_form("test_form") + form = form_handler.get_form(test_form_name) expect(form).to be_a(Form) expect(form.pages.count).to eq(27) end @@ -21,7 +22,7 @@ RSpec.describe FormHandler do it "should only load the form once at boot time" do form_handler = FormHandler.instance - expect(Form).not_to receive(:new).with("test_form") - expect(form_handler.get_form("test_form")).to be_a(Form) + expect(Form).not_to receive(:new).with(:any, test_form_name) + expect(form_handler.get_form(test_form_name)).to be_a(Form) end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index fa14e8797..1816946d4 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -1,9 +1,8 @@ require "rails_helper" RSpec.describe Form, type: :model do - form_handler = FormHandler.instance - let(:form) { form_handler.get_form("test_form") } let(:case_log) { FactoryBot.build(:case_log, :in_progress) } + let(:form) { case_log.form } let(:completed_case_log) { FactoryBot.build(:case_log, :completed) } let(:conditional_section_complete_case_log) { FactoryBot.build(:case_log, :conditional_section_complete) } diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index c46394742..bd783b8f4 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -178,10 +178,6 @@ RSpec.describe CaseLogsController, type: :request do context "edit log" do let(:headers) { { "Accept" => "text/html" } } - let(:form) { Form.new("spec/fixtures/forms/test_form.json") } - before do - allow(FormHandler.instance).to receive(:get_form).and_return(form) - end context "a user that is not signed in" do it "does not let the user get case log tasklist pages they don't have access to" do diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index ee9115184..872b798e3 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -69,7 +69,6 @@ RSpec.describe FormController, type: :request do describe "Submit Form" do context "a form page" do let(:user) { FactoryBot.create(:user) } - let(:form) { Form.new("spec/fixtures/forms/test_form.json") } let(:organisation) { user.organisation } let(:case_log) do FactoryBot.create( @@ -90,7 +89,6 @@ RSpec.describe FormController, type: :request do end before do - allow(FormHandler.instance).to receive(:get_form).and_return(form) post "/logs/#{case_log.id}/form", params: params end