Fix #276 Rewrite #dossiers_with_notifications_count_for_procedure and add tests

This commit is contained in:
Mathieu Magnin 2017-05-12 12:12:57 +02:00
parent 8f5fdfa81f
commit 58abd9eaf1
2 changed files with 19 additions and 10 deletions

View file

@ -87,15 +87,8 @@ class Gestionnaire < ActiveRecord::Base
end
def dossiers_with_notifications_count_for_procedure(procedure)
procedure_ids = dossiers_follow.pluck(:procedure_id)
if procedure_ids.include?(procedure.id)
return dossiers_follow.where(procedure_id: procedure.id)
.inject(0) do |acc, dossier|
acc += ((dossier.notifications.where(already_read: false).count) > 0 ? 1 : 0)
end
end
0
followed_dossiers_id = dossiers_follow.where(procedure: procedure).pluck(:id)
Notification.unread.where(dossier_id: followed_dossiers_id).select(:dossier_id).distinct(:dossier_id).count
end
def dossiers_with_notifications_count

View file

@ -336,12 +336,28 @@ describe Gestionnaire, type: :model do
it { is_expected.to eq(0) }
end
context 'with 2 notification' do
context 'with 2 notifications' do
let!(:notification){ create(:notification, already_read: false, dossier: dossier) }
let!(:notification2){ create(:notification, already_read: false, dossier: dossier) }
it { is_expected.to eq(1) }
end
context 'with another dossier' do
let!(:dossier2){create(:dossier, procedure: procedure, state: 'received')}
let!(:follow2){ create(:follow, dossier: dossier2, gestionnaire: gestionnaire) }
context 'and some notifications' do
let!(:notification){ create(:notification, already_read: false, dossier: dossier) }
let!(:notification2){ create(:notification, already_read: false, dossier: dossier) }
let!(:notification3){ create(:notification, already_read: false, dossier: dossier) }
let!(:notification4){ create(:notification, already_read: false, dossier: dossier2) }
let!(:notification5){ create(:notification, already_read: false, dossier: dossier2) }
it { is_expected.to eq(2) }
end
end
end
end
end