From daf1211510fa433410bb8f9b67ab179904b9f068 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Tue, 11 Jan 2022 11:53:35 +0000 Subject: [PATCH] Data collection windows don't follow the calendar year (#209) --- app/models/case_log.rb | 10 ++++++++-- app/models/form_handler.rb | 2 +- spec/factories/case_log.rb | 2 +- spec/models/case_log_spec.rb | 4 +++- spec/requests/form_controller_spec.rb | 4 ++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 82ca6f582..a1cd948c4 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -142,8 +142,14 @@ class CaseLog < ApplicationRecord end def form_name - start_year = year || 2021 - "#{start_year}_#{start_year + 1}" + return "2021_2022" unless startdate + + window_end_date = Time.zone.local(startdate.year, 4, 1) + if startdate < window_end_date + "#{startdate.year - 1}_#{startdate.year}" + else + "#{startdate.year}_#{startdate.year + 1}" + end end def self.editable_fields diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index bfceff1a9..8228077ca 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -7,7 +7,7 @@ class FormHandler end def get_form(form) - @forms[form] ||= Form.new(form) + @forms[form] end private diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 8a07b981c..b67e2aa1f 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -8,7 +8,7 @@ FactoryBot.define do tenant_same_property_renewal { "No" } needstype { 1 } rent_type { 1 } - startdate { Time.zone.local(2022, 1, 1) } + startdate { Time.zone.local(2022, 5, 1) } year { 2022 } end trait :in_progress do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 3b15a9ce3..d0bb4c718 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -10,9 +10,11 @@ RSpec.describe Form, type: :model do describe "#form" do let(:case_log) { FactoryBot.build(:case_log) } - let(:case_log_year_2) { FactoryBot.build(:case_log, year: 2023) } + let(:case_log_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2022, 1, 1)) } + let(:case_log_year_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2023, 5, 1)) } it "has returns the correct form based on the start date" do expect(case_log.form_name).to eq("2021_2022") + expect(case_log_2.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 diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 21c289bde..75c796b14 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -49,8 +49,8 @@ RSpec.describe FormController, type: :request do describe "GET" do context "form pages" do context "forms exist for multiple years" do - let(:case_log_year_1) { FactoryBot.create(:case_log, startdate: Time.zone.local(2021, 1, 1), owning_organisation: organisation) } - let(:case_log_year_2) { FactoryBot.create(:case_log, :about_completed, startdate: Time.zone.local(2022, 1, 1), owning_organisation: organisation) } + let(:case_log_year_1) { FactoryBot.create(:case_log, startdate: Time.zone.local(2021, 5, 1), owning_organisation: organisation) } + let(:case_log_year_2) { FactoryBot.create(:case_log, :about_completed, startdate: Time.zone.local(2022, 5, 1), owning_organisation: organisation) } it "displays the correct question details for each case log based on form year" do get "/logs/#{case_log_year_1.id}/tenant-code", headers: headers, params: {}