Merge pull request #7114 from tchak/refactor-dossiers-with-procedure-presentation

refactor(procedure_presentation): expose filtered_sorted_ids method
This commit is contained in:
Paul Chavard 2022-04-05 15:54:39 +02:00 committed by GitHub
commit 9a3b9e3912
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 68 deletions

View file

@ -57,12 +57,13 @@ module Instructeurs
@current_filters = current_filters @current_filters = current_filters
@displayed_fields_options, @displayed_fields_selected = procedure_presentation.displayed_fields_for_select @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) .dossiers_count_summary(groupe_instructeur_ids)
.fetch_values('a_suivre', 'suivis', 'traites', 'tous', 'supprimes_recemment', 'archives', 'expirant') .symbolize_keys
@can_download_dossiers = (@tous_count + @archives_count) > 0 @can_download_dossiers = (@counts[:tous] + @counts[:archives]) > 0
dossiers = Dossier.where(groupe_instructeur_id: groupe_instructeur_ids) dossiers = Dossier.where(groupe_instructeur_id: groupe_instructeur_ids)
dossiers_count = @counts[statut.underscore.to_sym]
@followed_dossiers_id = current_instructeur @followed_dossiers_id = current_instructeur
.followed_dossiers .followed_dossiers
@ -70,37 +71,12 @@ module Instructeurs
.merge(dossiers.visible_by_administration) .merge(dossiers.visible_by_administration)
.pluck(:id) .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) notifications = current_instructeur.notifications_for_groupe_instructeurs(groupe_instructeur_ids)
@has_en_cours_notifications = notifications[:en_cours].present? @has_en_cours_notifications = notifications[:en_cours].present?
@has_termine_notifications = notifications[:termines].present? @has_termine_notifications = notifications[:termines].present?
@not_archived_notifications_dossier_ids = notifications[:en_cours] + notifications[:termines] @not_archived_notifications_dossier_ids = notifications[:en_cours] + notifications[:termines]
sorted_ids = procedure_presentation.sorted_ids(@dossiers, dossiers_count) filtered_sorted_ids = procedure_presentation.filtered_sorted_ids(dossiers, dossiers_count, statut)
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
page = params[:page].presence || 1 page = params[:page].presence || 1

View file

@ -180,6 +180,17 @@ class ProcedurePresentation < ApplicationRecord
end.reduce(:&) end.reduce(:&)
end 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) def human_value_for_filter(filter)
case filter[TABLE] case filter[TABLE]
when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE

View file

@ -15,13 +15,13 @@
.container.flex= render partial: "tabs", locals: { procedure: @procedure, .container.flex= render partial: "tabs", locals: { procedure: @procedure,
statut: @statut, statut: @statut,
a_suivre_count: @a_suivre_count, a_suivre_count: @counts[:a_suivre],
suivis_count: @suivis_count, suivis_count: @counts[:suivis],
traites_count: @traites_count, traites_count: @counts[:traites],
tous_count: @tous_count, tous_count: @counts[:tous],
supprimes_recemment_count: @supprimes_recemment_count, supprimes_recemment_count: @counts[:supprimes_recemment],
archives_count: @archives_count, archives_count: @counts[:archives],
expirant_count: @expirant_count, expirant_count: @counts[:expirant],
has_en_cours_notifications: @has_en_cours_notifications, has_en_cours_notifications: @has_en_cours_notifications,
has_termine_notifications: @has_termine_notifications } has_termine_notifications: @has_termine_notifications }

View file

@ -273,21 +273,24 @@ describe Instructeurs::ProceduresController, type: :controller do
context 'with a new dossier without follower' do context 'with a new dossier without follower' do
let!(:new_unfollow_dossier) { create(:dossier, :en_instruction, procedure: procedure) } let!(:new_unfollow_dossier) { create(:dossier, :en_instruction, procedure: procedure) }
before { subject } context do
it { expect(assigns(:dossiers)).to match_array([new_unfollow_dossier]) } 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 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) } let!(:hidden_dossier) { create(:dossier, :en_construction, groupe_instructeur: gi_2, hidden_by_user_at: 1.hour.ago) }
before { subject } 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 end
context 'with a dossier en contruction not hidden by user' do context 'with a dossier en contruction not hidden by user' do
let!(:en_construction_dossier) { create(:dossier, :en_construction, groupe_instructeur: gi_2) } let!(:en_construction_dossier) { create(:dossier, :en_construction, groupe_instructeur: gi_2) }
before { subject } 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 end
context 'and dossiers without follower on each of the others groups' do context 'and dossiers without follower on each of the others groups' do
@ -296,32 +299,27 @@ describe Instructeurs::ProceduresController, type: :controller do
before { subject } 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
end end
context 'with a new dossier with a follower' do context 'with a new dossier with a follower' do
let(:statut) { 'suivis' } 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 context do
instructeur.followed_dossiers << new_followed_dossier before { subject }
subject
it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_followed_dossier].map(&:id)) }
end end
it { expect(assigns(:dossiers)).to match_array([new_followed_dossier]) }
context 'and dossier with a follower on each of the others groups' do 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_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) } let!(:new_follow_dossier_on_gi_3) { create(:dossier, :en_instruction, groupe_instructeur: gi_3, followers_instructeurs: [instructeur]) }
before do before { subject }
instructeur.followed_dossiers << new_follow_dossier_on_gi_2 << new_follow_dossier_on_gi_3
subject
end
# followed dossiers on another groupe should not be displayed it { expect(assigns(:filtered_sorted_paginated_ids)).to match_array([new_followed_dossier, new_follow_dossier_on_gi_2].map(&:id)) }
it { expect(assigns(:dossiers)).to contain_exactly(new_followed_dossier, new_follow_dossier_on_gi_2) }
end end
end end
@ -329,9 +327,11 @@ describe Instructeurs::ProceduresController, type: :controller do
let(:statut) { 'traites' } let(:statut) { 'traites' }
let!(:termine_dossier) { create(:dossier, :accepte, procedure: procedure) } 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 context 'and terminer dossiers on each of the others groups' do
let!(:termine_dossier_on_gi_2) { create(:dossier, :accepte, groupe_instructeur: gi_2) } 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 } 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
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) { 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) } 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 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) } 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 } 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
end end
@ -370,7 +372,7 @@ describe Instructeurs::ProceduresController, type: :controller do
before { subject } 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 end
describe 'statut' do describe 'statut' do
@ -387,7 +389,7 @@ describe Instructeurs::ProceduresController, type: :controller do
context 'when statut is empty' do context 'when statut is empty' do
let(:statut) { nil } 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') } it { expect(assigns(:statut)).to eq('a-suivre') }
end end
@ -395,35 +397,35 @@ describe Instructeurs::ProceduresController, type: :controller do
let(:statut) { 'a-suivre' } let(:statut) { 'a-suivre' }
it { expect(assigns(:statut)).to eq('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 end
context 'when statut is suivis' do context 'when statut is suivis' do
let(:statut) { 'suivis' } let(:statut) { 'suivis' }
it { expect(assigns(:statut)).to eq('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 end
context 'when statut is traites' do context 'when statut is traites' do
let(:statut) { 'traites' } let(:statut) { 'traites' }
it { expect(assigns(:statut)).to eq('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 end
context 'when statut is tous' do context 'when statut is tous' do
let(:statut) { 'tous' } let(:statut) { 'tous' }
it { expect(assigns(:statut)).to eq('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 end
context 'when statut is archives' do context 'when statut is archives' do
let(:statut) { 'archives' } let(:statut) { 'archives' }
it { expect(assigns(:statut)).to eq('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 end
end end