Commit b0bf0012 authored by Sean Arnold's avatar Sean Arnold

Add active_particpants scope

Add specs
parent 0cf2eadc
......@@ -32,6 +32,7 @@ module IncidentManagement
belongs_to :schedule, class_name: 'OncallSchedule', inverse_of: 'rotations', foreign_key: 'oncall_schedule_id'
# Note! If changing the order of participants, also change the :with_shift_generation_associations scope.
has_many :participants, -> { order(id: :asc) }, class_name: 'OncallParticipant', inverse_of: :rotation
has_many :active_participants, -> { not_removed.order(id: :asc) }, class_name: 'OncallParticipant', inverse_of: :rotation
has_many :users, through: :participants
has_many :shifts, class_name: 'OncallShift', inverse_of: :rotation, foreign_key: :rotation_id
......@@ -49,9 +50,9 @@ module IncidentManagement
scope :in_progress, -> { where('starts_at < :time AND (ends_at > :time OR ends_at IS NULL)', time: Time.current) }
scope :except_ids, -> (ids) { where.not(id: ids) }
scope :with_shift_generation_associations, -> do
joins(:participants, :schedule)
joins(:active_participants, :schedule)
.distinct
.includes(:participants, :schedule)
.includes(:active_participants, :schedule)
.order(:id, 'incident_management_oncall_participants.id ASC')
end
......
......@@ -8,8 +8,19 @@ RSpec.describe IncidentManagement::OncallRotation do
describe '.associations' do
it { is_expected.to belong_to(:schedule).class_name('OncallSchedule').inverse_of(:rotations) }
it { is_expected.to have_many(:participants).order(id: :asc).class_name('OncallParticipant').inverse_of(:rotation) }
it { is_expected.to have_many(:active_participants).order(id: :asc).class_name('OncallParticipant').inverse_of(:rotation) }
it { is_expected.to have_many(:users).through(:participants) }
it { is_expected.to have_many(:shifts).class_name('OncallShift').inverse_of(:rotation) }
describe '.active_participants' do
let(:rotation) { create(:incident_management_oncall_rotation, schedule: schedule) }
let(:participant) { create(:incident_management_oncall_participant, rotation: rotation) }
let(:removed_participant) { create(:incident_management_oncall_participant, :removed, rotation: rotation) }
subject { rotation.active_participants }
it { is_expected.to contain_exactly(participant) }
end
end
describe '.validations' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment