From a91c8da62ee9de5ce6be6f21b1cd02c617e78a57 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 30 Nov 2022 11:07:24 +0000 Subject: [PATCH] Update available_from methods (#1026) * Update available_from methods * Move collection start date logic to form handler --- app/models/form_handler.rb | 5 +++ app/models/location.rb | 4 ++- app/models/scheme.rb | 2 +- spec/helpers/locations_helper_spec.rb | 4 +-- spec/models/location_spec.rb | 44 +++++++++++++++++++++++++++ spec/models/scheme_spec.rb | 34 +++++++++++++++++++++ 6 files changed, 89 insertions(+), 4 deletions(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 61b981436..03de5e290 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -49,6 +49,11 @@ class FormHandler today < window_end_date ? today.year - 1 : today.year end + def collection_start_date(date) + window_end_date = Time.zone.local(date.year, 4, 1) + date < window_end_date ? Time.zone.local(date.year - 1, 4, 1) : Time.zone.local(date.year, 4, 1) + end + def current_collection_start_date Time.zone.local(current_collection_start_year, 4, 1) end diff --git a/app/models/location.rb b/app/models/location.rb index 46cb1da6c..4fa161e0b 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -370,7 +370,9 @@ class Location < ApplicationRecord end def available_from - startdate || [created_at, FormHandler.instance.current_collection_start_date].min + return startdate if startdate.present? + + FormHandler.instance.collection_start_date(created_at) end def status diff --git a/app/models/scheme.rb b/app/models/scheme.rb index fce696bb6..73aecd6ac 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -210,7 +210,7 @@ class Scheme < ApplicationRecord end def available_from - [created_at, FormHandler.instance.current_collection_start_date].min + FormHandler.instance.collection_start_date(created_at) end def status diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 4d9340542..c80449ce6 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -154,11 +154,11 @@ RSpec.describe LocationsHelper do context "when viewing availability" do context "with no deactivations" do - it "displays created_at as availability date if startdate is not present" do + it "displays previous collection start date as availability date if created_at is earlier than collection start date" do location.update!(startdate: nil) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] - expect(availability_attribute).to eq("Active from #{location.created_at.to_formatted_s(:govuk_date)}") + expect(availability_attribute).to eq("Active from 1 April 2021") end it "displays current collection start date as availability date if created_at is later than collection start date" do diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index dd5d697e9..58bd3872e 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -208,4 +208,48 @@ RSpec.describe Location, type: :model do end end end + + describe "available_from" do + context "when there is a startdate" do + let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 6)) } + + it "returns the startdate" do + expect(location.available_from).to eq(Time.zone.local(2022, 4, 6)) + end + end + + context "when there is no start date" do + context "and the location was created at the start of the 2022/23 collection window" do + let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2022, 4, 6), startdate: nil) } + + it "returns the beginning of 22/23 collection window" do + expect(location.available_from).to eq(Time.zone.local(2022, 4, 1)) + end + end + + context "and the location was created at the end of the 2022/23 collection window" do + let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2023, 2, 6), startdate: nil) } + + it "returns the beginning of 22/23 collection window" do + expect(location.available_from).to eq(Time.zone.local(2022, 4, 1)) + end + end + + context "and the location was created at the start of the 2021/22 collection window" do + let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2021, 4, 6), startdate: nil) } + + it "returns the beginning of 21/22 collection window" do + expect(location.available_from).to eq(Time.zone.local(2021, 4, 1)) + end + end + + context "and the location was created at the end of the 2021/22 collection window" do + let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2022, 2, 6), startdate: nil) } + + it "returns the beginning of 21/22 collection window" do + expect(location.available_from).to eq(Time.zone.local(2021, 4, 1)) + end + end + end + end end diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index 43a4112d4..d6bacb11a 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -190,4 +190,38 @@ RSpec.describe Scheme, type: :model do expect(all_schemes[2].status).to eq(:incomplete) end end + + describe "available_from" do + context "when the scheme was created at the start of the 2022/23 collection window" do + let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2022, 4, 6)) } + + it "returns the beginning of 22/23 collection window" do + expect(scheme.available_from).to eq(Time.zone.local(2022, 4, 1)) + end + end + + context "when the scheme was created at the end of the 2022/23 collection window" do + let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2023, 2, 6)) } + + it "returns the beginning of 22/23 collection window" do + expect(scheme.available_from).to eq(Time.zone.local(2022, 4, 1)) + end + end + + context "when the scheme was created at the start of the 2021/22 collection window" do + let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2021, 4, 6)) } + + it "returns the beginning of 21/22 collection window" do + expect(scheme.available_from).to eq(Time.zone.local(2021, 4, 1)) + end + end + + context "when the scheme was created at the end of the 2021/22 collection window" do + let(:scheme) { FactoryBot.build(:scheme, created_at: Time.zone.local(2022, 2, 6)) } + + it "returns the beginning of 21/22 collection window" do + expect(scheme.available_from).to eq(Time.zone.local(2021, 4, 1)) + end + end + end end