From 562da0ed00667925035297fc8d34d7c270c3913c Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 21 Nov 2022 10:25:41 +0000 Subject: [PATCH] Move the active_periods into helper --- app/helpers/locations_helper.rb | 15 +++++- app/models/location.rb | 13 ----- spec/helpers/locations_helper_spec.rb | 77 +++++++++++++++++++++++++++ spec/models/location_spec.rb | 77 --------------------------- 4 files changed, 91 insertions(+), 91 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index e3b2b3bb4..c95e25d84 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -42,9 +42,22 @@ module LocationsHelper base_attributes end + ActivePeriod = Struct.new(:from, :to) + def active_periods(location) + periods = [ActivePeriod.new(location.available_from, nil)] + + sorted_deactivation_periods = location.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.from != period.to) && (period.to.nil? || (period.from.present? && period.from <= period.to)) } + end + def location_availability(location) availability = "" - location.active_periods.each do |period| + active_periods(location).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 de272a415..70a8c8ede 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -386,19 +386,6 @@ 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.from != period.to) && (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 94ab0bb1b..d2540481e 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -47,6 +47,83 @@ RSpec.describe LocationsHelper do 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(active_periods(location).count).to eq(1) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil) + end + + it "ignores reactivations that were deactivated on the same day" 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, 6, 4)) + location.save! + + expect(active_periods(location).count).to eq(1) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + 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(active_periods(location).count).to eq(2) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(active_periods(location).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(active_periods(location).count).to eq(3) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + expect(active_periods(location).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(active_periods(location).count).to eq(2) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(active_periods(location).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(active_periods(location).count).to eq(3) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 6, 4), to: Time.zone.local(2022, 7, 6)) + expect(active_periods(location).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(active_periods(location).count).to eq(2) + expect(active_periods(location).first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 4, 6)) + expect(active_periods(location).second).to have_attributes(from: Time.zone.local(2022, 11, 11), to: nil) + end + end + end + describe "display_location_attributes" do let(:location) { FactoryBot.build(:location, created_at: Time.zone.local(2022, 3, 16), startdate: Time.zone.local(2022, 4, 1)) } diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index e6b9d5a21..53d92013e 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -196,81 +196,4 @@ RSpec.describe Location, type: :model do 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 "ignores reactivations that were deactivated on the same day" 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, 6, 4)) - location.save! - - expect(location.active_periods.count).to eq(1) - expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) - 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 end