Browse Source

Remove active periods that last 1 day (because they get deactivated on the same day)

pull/1007/head
Kat 4 years ago
parent
commit
1119d98d89
  1. 4
      app/models/location.rb
  2. 9
      spec/models/location_spec.rb

4
app/models/location.rb

@ -396,7 +396,7 @@ class Location < ApplicationRecord
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)) }
periods.select { |period| (period.from != period.to) && (period.to.nil? || (period.from.present? && period.from <= period.to)) }
end
def active?
@ -424,7 +424,7 @@ class Location < ApplicationRecord
elsif deactivation_date_type == "other"
errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.invalid"))
end
elsif location_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date) }
elsif location_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) }
errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation.during_deactivated_period"))
else
unless deactivation_date.between?(available_from, Time.zone.local(2200, 1, 1))

9
spec/models/location_spec.rb

@ -214,6 +214,15 @@ RSpec.describe Location, type: :model do
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))

Loading…
Cancel
Save