diff --git a/app/helpers/collection_time_helper.rb b/app/helpers/collection_time_helper.rb new file mode 100644 index 000000000..8478d06e7 --- /dev/null +++ b/app/helpers/collection_time_helper.rb @@ -0,0 +1,16 @@ +module CollectionTimeHelper + def current_collection_start_year + today = Time.zone.now + window_end_date = Time.zone.local(today.year, 4, 1) + 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 +end diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 51a0b6177..098a7b429 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -1,5 +1,6 @@ class FormHandler include Singleton + include CollectionTimeHelper attr_reader :forms def initialize @@ -44,21 +45,6 @@ class FormHandler forms end - def current_collection_start_year - today = Time.zone.now - window_end_date = Time.zone.local(today.year, 4, 1) - 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 - def form_name_from_start_year(year, type) form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" } form_mappings[current_collection_start_year - year] diff --git a/spec/helpers/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb new file mode 100644 index 000000000..3b02802f2 --- /dev/null +++ b/spec/helpers/collection_time_helper_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +RSpec.describe CollectionTimeHelper do + let(:current_user) { create(:user, :data_coordinator) } + let(:user) { create(:user, :data_coordinator) } + + around do |example| + Timecop.freeze(now) do + example.run + end + end + + describe "Current collection start year" do + context "when the date is after 1st of April" do + let(:now) { Time.utc(2022, 8, 3) } + + it "returns the same year as the current start year" do + expect(current_collection_start_year).to eq(2022) + end + + it "returns the correct current start date" do + expect(current_collection_start_date).to eq(Time.zone.local(2022, 4, 1)) + end + end + + context "with the date before 1st of April" do + let(:now) { Time.utc(2022, 2, 3) } + + it "returns the previous year as the current start year" do + expect(current_collection_start_year).to eq(2021) + end + end + end +end diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 89f550bee..30a75f605 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -76,10 +76,6 @@ RSpec.describe FormHandler do context "when the date is after 1st of April" do let(:now) { Time.utc(2022, 8, 3) } - it "returns the same year as the current start year" do - expect(form_handler.current_collection_start_year).to eq(2022) - end - it "returns the correct current lettings form name" do expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("current_lettings") end @@ -103,19 +99,11 @@ RSpec.describe FormHandler do it "returns the correct next sales form name" do expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("next_sales") end - - it "returns the correct current start date" do - expect(form_handler.current_collection_start_date).to eq(Time.zone.local(2022, 4, 1)) - end end context "with the date before 1st of April" do let(:now) { Time.utc(2022, 2, 3) } - it "returns the previous year as the current start year" do - expect(form_handler.current_collection_start_year).to eq(2021) - end - it "returns the correct current lettings form name" do expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("current_lettings") end