Browse Source

Refactor active_period method into the model

pull/1007/head
Kat 4 years ago
parent
commit
1c38e24612
  1. 12
      app/helpers/locations_helper.rb
  2. 13
      app/models/location.rb
  3. 2
      spec/helpers/locations_helper_spec.rb
  4. 77
      spec/models/location_spec.rb

12
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?

13
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

2
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]

77
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

Loading…
Cancel
Save