From 1c38e24612f1ff6b2640defa75089b7a4960c647 Mon Sep 17 00:00:00 2001 From: Kat Date: Sun, 20 Nov 2022 12:40:41 +0000 Subject: [PATCH] Refactor active_period method into the model --- app/helpers/locations_helper.rb | 12 +---- app/models/location.rb | 13 +++++ spec/helpers/locations_helper_spec.rb | 2 +- spec/models/location_spec.rb | 77 ++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 5e2b5ee66..e3b2b3bb4 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -42,19 +42,9 @@ module LocationsHelper base_attributes end - ActivePeriod = Struct.new(:from, :to) def location_availability(location) - active_periods = [ActivePeriod.new(location.available_from, nil)] - - sorted_deactivation_periods = location.location_deactivation_periods.sort_by(&:deactivation_date) - sorted_deactivation_periods.each do |deactivation| - active_periods.find { |x| x.to.nil? }.to = deactivation.deactivation_date - active_periods << ActivePeriod.new(deactivation.reactivation_date, nil) - end - - filtered_active_periods = active_periods.select { |period| period.to.nil? || (period.from.present? && period.from <= period.to) } availability = "" - filtered_active_periods.each do |period| + location.active_periods.each do |period| if period.from.present? availability << "\nActive from #{period.from.to_formatted_s(:govuk_date)}" availability << " to #{(period.to - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{period.to.to_formatted_s(:govuk_date)}" if period.to.present? diff --git a/app/models/location.rb b/app/models/location.rb index 1b50f7aba..5d5c2f9da 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -386,6 +386,19 @@ class Location < ApplicationRecord :active end + ActivePeriod = Struct.new(:from, :to) + def active_periods + periods = [ActivePeriod.new(available_from, nil)] + + sorted_deactivation_periods = location_deactivation_periods.sort_by(&:deactivation_date) + sorted_deactivation_periods.each do |deactivation| + periods.find { |x| x.to.nil? }.to = deactivation.deactivation_date + periods << ActivePeriod.new(deactivation.reactivation_date, nil) + end + + periods.select { |period| (period.to.present? || period.from.present?) && (period.to.nil? || (period.from.present? && period.from <= period.to)) } + end + def active? status == :active end diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index cd08c8d7c..36a68a92e 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -67,7 +67,7 @@ RSpec.describe LocationsHelper do end context "when viewing availability" do - context "with are no deactivations" do + context "with ae no deactivations" do it "displays created_at as availability date if startdate is not present" do location.update!(startdate: nil) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index fc8cb9ac2..11f779305 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -155,7 +155,7 @@ RSpec.describe Location, type: :model do context "when there have been previous deactivations" do before do - location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 4), reactivation_date: Time.zone.local(2022, 6, 5)) location.save! end @@ -187,6 +187,81 @@ RSpec.describe Location, type: :model do location.save! expect(location.status).to eq(:reactivating_soon) end + + it "returns if the location had a deactivation during another deactivation" do + Timecop.freeze(2022, 6, 4) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2)) + location.save! + expect(location.status).to eq(:reactivating_soon) + end + end + end + + describe "Active periods" do + let(:location) { FactoryBot.create(:location, startdate: nil) } + + before do + Timecop.freeze(2022, 10, 10) + end + + after do + Timecop.unfreeze + end + + context "when there have not been any previous deactivations" do + it "returns one active period without to date" do + expect(location.active_periods.count).to eq(1) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil) + end + + it "returns sequential non reactivated active periods" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6)) + location.save! + + expect(location.active_periods.count).to eq(2) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(location.active_periods.second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + end + + it "returns sequential reactivated active periods" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5)) + location.save! + expect(location.active_periods.count).to eq(3) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(location.active_periods.second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + expect(location.active_periods.third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + end + + it "returns non sequential non reactivated active periods" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: nil) + location.save! + + expect(location.active_periods.count).to eq(2) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(location.active_periods.second).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + end + + it "returns non sequential reactivated active periods" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6), reactivation_date: Time.zone.local(2022, 8, 5)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4)) + location.save! + expect(location.active_periods.count).to eq(3) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(location.active_periods.second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + expect(location.active_periods.third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) + end + + it "returns correct active periods when reactivation happends during a deactivated period" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 11, 11)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7)) + + expect(location.active_periods.count).to eq(2) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6)) + expect(location.active_periods.second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil) + end end end