From 8f5fdfa81f0a44d516697ebf5481090428b8d1b6 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 12 May 2017 11:35:01 +0200 Subject: [PATCH 1/2] Rename dossier_with_notification_for --- app/facades/dossiers_list_facades.rb | 2 +- app/models/gestionnaire.rb | 2 +- .../_left_panel_backoffice_dossierscontroller_index.html.haml | 2 +- spec/models/gestionnaire_spec.rb | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/facades/dossiers_list_facades.rb b/app/facades/dossiers_list_facades.rb index 016d007d8..9ab6b6055 100644 --- a/app/facades/dossiers_list_facades.rb +++ b/app/facades/dossiers_list_facades.rb @@ -30,7 +30,7 @@ class DossiersListFacades end def gestionnaire_procedures_name_and_id_list - @current_devise_profil.procedures.order('libelle ASC').inject([]) { |acc, procedure| acc.push({id: procedure.id, libelle: procedure.libelle, unread_notifications: @current_devise_profil.dossier_with_notification_for(procedure)}) } + @current_devise_profil.procedures.order('libelle ASC').inject([]) { |acc, procedure| acc.push({id: procedure.id, libelle: procedure.libelle, unread_notifications: @current_devise_profil.dossiers_with_notifications_count_for_procedure(procedure)}) } end def unread_notifications diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index d646d7177..c45386825 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -86,7 +86,7 @@ class Gestionnaire < ActiveRecord::Base 0 end - def dossier_with_notification_for procedure + def dossiers_with_notifications_count_for_procedure(procedure) procedure_ids = dossiers_follow.pluck(:procedure_id) if procedure_ids.include?(procedure.id) diff --git a/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml b/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml index 320dadc79..60f5ed36b 100644 --- a/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml +++ b/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml @@ -28,7 +28,7 @@ - if total_new > 0 .badge.progress-bar-success{ title: 'Nouveaux dossiers' } = total_new - - unread_notif_count = current_gestionnaire.dossier_with_notification_for(procedure) + - unread_notif_count = current_gestionnaire.dossiers_with_notifications_count_for_procedure(procedure) - if unread_notif_count > 0 .badge.progress-bar-warning{ title: 'Notifications' } = unread_notif_count diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index b3a20f6d6..9414266f3 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -313,8 +313,8 @@ describe Gestionnaire, type: :model do end end - describe '#dossier_with_notification_for' do - subject { gestionnaire.dossier_with_notification_for(procedure) } + describe '#dossiers_with_notifications_count_for_procedure' do + subject { gestionnaire.dossiers_with_notifications_count_for_procedure(procedure) } context 'without notifications' do it { is_expected.to eq(0) } From 58abd9eaf161d879e24124c41abda4f2f71d3f4d Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 12 May 2017 12:12:57 +0200 Subject: [PATCH 2/2] Fix #276 Rewrite #dossiers_with_notifications_count_for_procedure and add tests --- app/models/gestionnaire.rb | 11 ++--------- spec/models/gestionnaire_spec.rb | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index c45386825..6dda6ed7a 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -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 diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index 9414266f3..5b286ceaa 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -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