From ec3ed049efa2b95d9cd145de341a42212703fd43 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 31 Mar 2022 18:28:12 +0200 Subject: [PATCH] refactor(procedure_presentation): expose filtered_sorted_ids method --- .../instructeurs/procedures_controller.rb | 34 ++-------- app/models/procedure_presentation.rb | 11 ++++ .../instructeurs/procedures/show.html.haml | 14 ++-- .../procedures_controller_spec.rb | 66 ++++++++++--------- 4 files changed, 57 insertions(+), 68 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 641bc076d..7dbdc2365 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -57,12 +57,13 @@ module Instructeurs @current_filters = current_filters @displayed_fields_options, @displayed_fields_selected = procedure_presentation.displayed_fields_for_select - @a_suivre_count, @suivis_count, @traites_count, @tous_count, @supprimes_recemment_count, @archives_count, @expirant_count = current_instructeur + @counts = current_instructeur .dossiers_count_summary(groupe_instructeur_ids) - .fetch_values('a_suivre', 'suivis', 'traites', 'tous', 'supprimes_recemment', 'archives', 'expirant') - @can_download_dossiers = (@tous_count + @archives_count) > 0 + .symbolize_keys + @can_download_dossiers = (@counts[:tous] + @counts[:archives]) > 0 dossiers = Dossier.where(groupe_instructeur_id: groupe_instructeur_ids) + dossiers_count = @counts[statut.underscore.to_sym] @followed_dossiers_id = current_instructeur .followed_dossiers @@ -70,37 +71,12 @@ module Instructeurs .merge(dossiers.visible_by_administration) .pluck(:id) - @dossiers = dossiers.by_statut(current_instructeur, statut) - dossiers_count = case statut - when 'a-suivre' - @a_suivre_count - when 'suivis' - @suivis_count - when 'traites' - @traites_count - when 'tous' - @tous_count - when 'supprimes_recemment' - @supprimes_recemment_count - when 'archives' - @archives_count - when 'expirant' - @expirant_count - end - notifications = current_instructeur.notifications_for_groupe_instructeurs(groupe_instructeur_ids) @has_en_cours_notifications = notifications[:en_cours].present? @has_termine_notifications = notifications[:termines].present? @not_archived_notifications_dossier_ids = notifications[:en_cours] + notifications[:termines] - sorted_ids = procedure_presentation.sorted_ids(@dossiers, dossiers_count) - - if @current_filters.count > 0 - filtered_ids = procedure_presentation.filtered_ids(@dossiers, statut) - filtered_sorted_ids = sorted_ids.filter { |id| filtered_ids.include?(id) } - else - filtered_sorted_ids = sorted_ids - end + filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, dossiers_count, statut) page = params[:page].presence || 1 diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 4a4a29bca..1c39669d5 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -180,6 +180,17 @@ class ProcedurePresentation < ApplicationRecord end.reduce(:&) end + def filtered_sorted_ids(dossiers, count, statut) + dossiers_by_statut = dossiers.by_statut(instructeur, statut) + dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, count) + + if filters[statut].present? + filtered_ids(dossiers_by_statut, statut).intersection(dossiers_sorted_ids) + else + dossiers_sorted_ids + end + end + def human_value_for_filter(filter) case filter[TABLE] when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index e9d171bb5..ac166b19d 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -15,13 +15,13 @@ .container.flex= render partial: "tabs", locals: { procedure: @procedure, statut: @statut, - a_suivre_count: @a_suivre_count, - suivis_count: @suivis_count, - traites_count: @traites_count, - tous_count: @tous_count, - supprimes_recemment_count: @supprimes_recemment_count, - archives_count: @archives_count, - expirant_count: @expirant_count, + a_suivre_count: @counts[:a_suivre], + suivis_count: @counts[:suivis], + traites_count: @counts[:traites], + tous_count: @counts[:tous], + supprimes_recemment_count: @counts[:supprimes_recemment], + archives_count: @counts[:archives], + expirant_count: @counts[:expirant], has_en_cours_notifications: @has_en_cours_notifications, has_termine_notifications: @has_termine_notifications } diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 937414ef6..57e492fcb 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -273,21 +273,24 @@ describe Instructeurs::ProceduresController, type: :controller do context 'with a new dossier without follower' do let!(:new_unfollow_dossier) { create(:dossier, :en_instruction, procedure: procedure) } - before { subject } - it { expect(assigns(:dossiers)).to match_array([new_unfollow_dossier]) } + context do + before { subject } + + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_unfollow_dossier].map(&:id)) } + end context 'with a dossier en contruction hidden by user' do let!(:hidden_dossier) { create(:dossier, :en_construction, groupe_instructeur: gi_2, hidden_by_user_at: 1.hour.ago) } before { subject } - it { expect(assigns(:dossiers)).to match_array([new_unfollow_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_unfollow_dossier].map(&:id)) } end context 'with a dossier en contruction not hidden by user' do let!(:en_construction_dossier) { create(:dossier, :en_construction, groupe_instructeur: gi_2) } before { subject } - it { expect(assigns(:dossiers)).to match_array([new_unfollow_dossier, en_construction_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_unfollow_dossier, en_construction_dossier].map(&:id)) } end context 'and dossiers without follower on each of the others groups' do @@ -296,32 +299,27 @@ describe Instructeurs::ProceduresController, type: :controller do before { subject } - it { expect(assigns(:dossiers)).to match_array([new_unfollow_dossier, new_unfollow_dossier_on_gi_2]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_unfollow_dossier, new_unfollow_dossier_on_gi_2].map(&:id)) } end end context 'with a new dossier with a follower' do let(:statut) { 'suivis' } - let!(:new_followed_dossier) { create(:dossier, :en_instruction, procedure: procedure) } + let!(:new_followed_dossier) { create(:dossier, :en_instruction, procedure: procedure, followers_instructeurs: [instructeur]) } - before do - instructeur.followed_dossiers << new_followed_dossier - subject + context do + before { subject } + + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_followed_dossier].map(&:id)) } end - it { expect(assigns(:dossiers)).to match_array([new_followed_dossier]) } - context 'and dossier with a follower on each of the others groups' do - let!(:new_follow_dossier_on_gi_2) { create(:dossier, :en_instruction, groupe_instructeur: gi_2) } - let!(:new_follow_dossier_on_gi_3) { create(:dossier, :en_instruction, groupe_instructeur: gi_3) } + let!(:new_follow_dossier_on_gi_2) { create(:dossier, :en_instruction, groupe_instructeur: gi_2, followers_instructeurs: [instructeur]) } + let!(:new_follow_dossier_on_gi_3) { create(:dossier, :en_instruction, groupe_instructeur: gi_3, followers_instructeurs: [instructeur]) } - before do - instructeur.followed_dossiers << new_follow_dossier_on_gi_2 << new_follow_dossier_on_gi_3 - subject - end + before { subject } - # followed dossiers on another groupe should not be displayed - it { expect(assigns(:dossiers)).to contain_exactly(new_followed_dossier, new_follow_dossier_on_gi_2) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_followed_dossier, new_follow_dossier_on_gi_2].map(&:id)) } end end @@ -329,9 +327,11 @@ describe Instructeurs::ProceduresController, type: :controller do let(:statut) { 'traites' } let!(:termine_dossier) { create(:dossier, :accepte, procedure: procedure) } - before { subject } + context do + before { subject } - it { expect(assigns(:dossiers)).to match_array([termine_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([termine_dossier].map(&:id)) } + end context 'and terminer dossiers on each of the others groups' do let!(:termine_dossier_on_gi_2) { create(:dossier, :accepte, groupe_instructeur: gi_2) } @@ -339,7 +339,7 @@ describe Instructeurs::ProceduresController, type: :controller do before { subject } - it { expect(assigns(:dossiers)).to match_array([termine_dossier, termine_dossier_on_gi_2]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([termine_dossier, termine_dossier_on_gi_2].map(&:id)) } end end @@ -348,9 +348,11 @@ describe Instructeurs::ProceduresController, type: :controller do let!(:archived_dossier) { create(:dossier, :en_instruction, procedure: procedure, archived: true) } let!(:archived_dossier_deleted) { create(:dossier, :en_instruction, procedure: procedure, archived: true, hidden_by_administration_at: 2.days.ago) } - before { subject } + context do + before { subject } - it { expect(assigns(:dossiers)).to match_array([archived_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([archived_dossier].map(&:id)) } + end context 'and terminer dossiers on each of the others groups' do let!(:archived_dossier_on_gi_2) { create(:dossier, :en_instruction, groupe_instructeur: gi_2, archived: true) } @@ -358,7 +360,7 @@ describe Instructeurs::ProceduresController, type: :controller do before { subject } - it { expect(assigns(:dossiers)).to match_array([archived_dossier, archived_dossier_on_gi_2]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([archived_dossier, archived_dossier_on_gi_2].map(&:id)) } end end @@ -370,7 +372,7 @@ describe Instructeurs::ProceduresController, type: :controller do before { subject } - it { expect(assigns(:dossiers)).to match_array([expiring_dossier_termine, expiring_dossier_en_construction]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([expiring_dossier_termine, expiring_dossier_en_construction].map(&:id)) } end describe 'statut' do @@ -387,7 +389,7 @@ describe Instructeurs::ProceduresController, type: :controller do context 'when statut is empty' do let(:statut) { nil } - it { expect(assigns(:dossiers)).to match_array([a_suivre_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([a_suivre_dossier].map(&:id)) } it { expect(assigns(:statut)).to eq('a-suivre') } end @@ -395,35 +397,35 @@ describe Instructeurs::ProceduresController, type: :controller do let(:statut) { 'a-suivre' } it { expect(assigns(:statut)).to eq('a-suivre') } - it { expect(assigns(:dossiers)).to match_array([a_suivre_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([a_suivre_dossier].map(&:id)) } end context 'when statut is suivis' do let(:statut) { 'suivis' } it { expect(assigns(:statut)).to eq('suivis') } - it { expect(assigns(:dossiers)).to match_array([new_followed_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_followed_dossier].map(&:id)) } end context 'when statut is traites' do let(:statut) { 'traites' } it { expect(assigns(:statut)).to eq('traites') } - it { expect(assigns(:dossiers)).to match_array([termine_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([termine_dossier].map(&:id)) } end context 'when statut is tous' do let(:statut) { 'tous' } it { expect(assigns(:statut)).to eq('tous') } - it { expect(assigns(:dossiers)).to match_array([a_suivre_dossier, new_followed_dossier, termine_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([a_suivre_dossier, new_followed_dossier, termine_dossier].map(&:id)) } end context 'when statut is archives' do let(:statut) { 'archives' } it { expect(assigns(:statut)).to eq('archives') } - it { expect(assigns(:dossiers)).to match_array([archived_dossier]) } + it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([archived_dossier].map(&:id)) } end end end