Performance: Only update specific calendars on destroy and not loop over all.
This commit is contained in:
parent
f2f36d0489
commit
b180a85e7f
2 changed files with 17 additions and 8 deletions
|
@ -373,18 +373,14 @@ returns
|
||||||
end
|
end
|
||||||
|
|
||||||
# check if sla's are refer to an existing calendar
|
# check if sla's are refer to an existing calendar
|
||||||
|
if destroyed?
|
||||||
default_calendar = Calendar.find_by(default: true)
|
default_calendar = Calendar.find_by(default: true)
|
||||||
Sla.find_each do |sla|
|
Sla.where(calendar_id: id).find_each do |sla|
|
||||||
if !sla.calendar_id
|
|
||||||
sla.calendar_id = default_calendar.id
|
|
||||||
sla.save!
|
|
||||||
next
|
|
||||||
end
|
|
||||||
if !Calendar.exists?(id: sla.calendar_id)
|
|
||||||
sla.calendar_id = default_calendar.id
|
sla.calendar_id = default_calendar.id
|
||||||
sla.save!
|
sla.save!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,19 @@ RSpec.describe Calendar, type: :model do
|
||||||
expect { described_class.first.destroy }
|
expect { described_class.first.destroy }
|
||||||
.to change { calendar.reload.default }.to(true)
|
.to change { calendar.reload.default }.to(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when sla has destroyed calendar set' do
|
||||||
|
let(:sla) { create(:sla, calendar: described_class.first) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
sla
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets the new default calendar to the sla' do
|
||||||
|
expect { described_class.first.destroy }
|
||||||
|
.to change { sla.reload.calendar }.to(calendar)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue