From 3877e48649b521af177e94ccc3ccb17daac2f312 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 18 Nov 2022 15:59:14 +0000 Subject: [PATCH] Filter out active periods --- app/helpers/locations_helper.rb | 5 +++-- spec/helpers/locations_helper_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 2035ebd95..b5d12fc5d 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -51,9 +51,10 @@ module LocationsHelper 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 = "" - active_periods.each do |period| + filtered_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/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 6ef8b06d8..cd08c8d7c 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -154,6 +154,19 @@ RSpec.describe LocationsHelper do end end end + + context "with intersecting deactivations" do + before do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 10), reactivation_date: Time.zone.local(2022, 12, 1)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 11, 11), reactivation_date: Time.zone.local(2022, 12, 11)) + end + + it "displays the timeline of availability" do + availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] + + expect(availability_attribute).to eq("Active from 1 April 2022 to 9 October 2022\nDeactivated on 10 October 2022\nActive from 11 December 2022") + end + end end end end