change active record request to get expert list

This commit is contained in:
kara Diaby 2021-02-11 19:32:48 +01:00 committed by Pierre de La Morinerie (Rebase PR Action)
parent 7b79fb2500
commit 7b4ec486ec
3 changed files with 11 additions and 16 deletions

View file

@ -186,7 +186,7 @@ module NewAdministrateur
end
def invited_expert_list
@invited_expert_emails = ExpertsProcedure.invited_expert_emails(@procedure)
@experts_procedure = @procedure.experts_procedures.sort_by { |expert_procedure| expert_procedure.expert.email }
end
private

View file

@ -14,15 +14,4 @@ class ExpertsProcedure < ApplicationRecord
belongs_to :procedure
has_many :avis, dependent: :destroy
def email_to_display
expert&.email
end
def self.invited_expert_emails(procedure)
joins(:expert)
.where(procedure: procedure)
.map(&:email_to_display)
.sort
end
end

View file

@ -7,7 +7,7 @@ RSpec.describe ExpertsProcedure, type: :model do
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:experts_procedure2) { ExpertsProcedure.create(expert: expert2, procedure: procedure) }
let(:experts_procedure3) { ExpertsProcedure.create(expert: expert3, procedure: procedure) }
subject { ExpertsProcedure.invited_expert_emails(procedure) }
subject { procedure.experts_procedures }
context 'when there is one dossier' do
let!(:dossier) { create(:dossier, procedure: procedure) }
@ -15,14 +15,18 @@ RSpec.describe ExpertsProcedure, type: :model do
context 'when a procedure has one avis and known instructeur' do
let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: expert.email), experts_procedure: experts_procedure) }
it { is_expected.to eq([expert.email]) }
it { is_expected.to eq([experts_procedure]) }
it { expect(procedure.experts.count).to eq(1) }
it { expect(procedure.experts.first.email).to eq(expert.email) }
end
context 'when a dossier has 2 avis from the same expert' do
let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) }
let!(:avis2) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) }
it { is_expected.to eq([expert.email]) }
it { is_expected.to eq([experts_procedure]) }
it { expect(procedure.experts.count).to eq(1) }
it { expect(procedure.experts.first).to eq(expert) }
end
end
@ -35,7 +39,9 @@ RSpec.describe ExpertsProcedure, type: :model do
let!(:avis2) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure2) }
let!(:avis3) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure3) }
it { is_expected.to eq([expert.email, expert2.email, expert3.email].sort) }
it { is_expected.to match_array([experts_procedure, experts_procedure2, experts_procedure3]) }
it { expect(procedure.experts.count).to eq(3) }
it { expect(procedure.experts).to match_array([expert, expert2, expert3]) }
end
end
end