From c96781cfad91d033caba7520b748a06c396e64c0 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 8 Nov 2022 15:59:00 +0100 Subject: [PATCH] list all admins --- app/models/procedures_filter.rb | 36 ++++- .../administrateurs/procedures/all.html.haml | 142 ++++++++++++------ spec/models/procedures_filter_spec.rb | 17 ++- 3 files changed, 137 insertions(+), 58 deletions(-) diff --git a/app/models/procedures_filter.rb b/app/models/procedures_filter.rb index 3564916ff..664439614 100644 --- a/app/models/procedures_filter.rb +++ b/app/models/procedures_filter.rb @@ -5,7 +5,7 @@ class ProceduresFilter def initialize(admin, params) @admin = admin - @params = params.permit(:page, :from_publication_date, zone_ids: [], statuses: []) + @params = params.permit(:page, :from_publication_date, :view_admins, zone_ids: [], statuses: []) end def admin_zones @@ -36,6 +36,10 @@ class ProceduresFilter nil end + def view_admins? + params[:view_admins] == 'true' + end + def zone_filtered?(zone_id) zone_ids&.map(&:to_i)&.include?(zone_id) end @@ -53,12 +57,32 @@ class ProceduresFilter end end + def with_view_admins(view_admins) + params.to_h.merge(view_admins: view_admins) + end + def procedures_result return @procedures_result if @procedures_result - @procedures_result = Procedure.joins(:procedures_zones).publiees_ou_closes - @procedures_result = @procedures_result.where(procedures_zones: { zone_id: zone_ids }) if zone_ids.present? - @procedures_result = @procedures_result.where(aasm_state: statuses) if statuses.present? - @procedures_result = @procedures_result.where('published_at >= ?', from_publication_date) if from_publication_date.present? - @procedures_result = @procedures_result.page(params[:page]).per(ITEMS_PER_PAGE).order(published_at: :desc) + @procedures_result = paginate(filter_procedures, published_at: :desc) + end + + def admins_result + return @admins_result if @admins_result + @admins_result = Administrateur.includes(:user).where(id: AdministrateursProcedure.where(procedure: filter_procedures).select(:administrateur_id)) + @admins_result = paginate(@admins_result, 'users.email') + end + + private + + def filter_procedures + procedures_result = Procedure.joins(:procedures_zones).publiees_ou_closes + procedures_result = procedures_result.where(procedures_zones: { zone_id: zone_ids }) if zone_ids.present? + procedures_result = procedures_result.where(aasm_state: statuses) if statuses.present? + procedures_result = procedures_result.where('published_at >= ?', from_publication_date) if from_publication_date.present? + procedures_result + end + + def paginate(result, ordered_by) + result.page(params[:page]).per(ITEMS_PER_PAGE).order(ordered_by) end end diff --git a/app/views/administrateurs/procedures/all.html.haml b/app/views/administrateurs/procedures/all.html.haml index 29a31201e..47669a100 100644 --- a/app/views/administrateurs/procedures/all.html.haml +++ b/app/views/administrateurs/procedures/all.html.haml @@ -64,52 +64,96 @@ = b.label(class: 'fr-label') { t b.text, scope: 'activerecord.attributes.procedure.aasm_state' } .fr-col-9 - .main-filter-header.fr-my-3w - .search - = link_to 'Voir la liste des administeurs', '#', class: 'fr-btn fr-btn--secondary btn-admins' - .fr-table.fr-table--bordered - %table#all-demarches - %caption - = "#{@filter.procedures_result.total_count} démarches" - %span.hidden.fr-icon-ball-pen-fill{ 'aria-hidden': 'true', 'data-autosubmit-target': 'spinner' } - - if @filter.selected_zones.present? - .selected-zones.fr-mb-2w - - @filter.selected_zones.each do |zone| - = link_to zone.current_label, all_admin_procedures_path(@filter.without(:zone_ids, zone.id)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' - - if @filter.statuses.present? - .selected-statuses.fr-mb-2w - - @filter.statuses.each do |status| - = link_to status, all_admin_procedures_path(@filter.without(:statuses, status)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' - - if @filter.from_publication_date.present? - .selected-from-publication-date.fr-mb-2w - = link_to "Depuis le #{l(@filter.from_publication_date)}", all_admin_procedures_path(@filter.without(:from_publication_date)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' - = paginate @filter.procedures_result, views_prefix: 'administrateurs' - %thead - %tr - %th{ scope: 'col' } - %th{ scope: 'col' } Démarche - %th{ scope: 'col' } N° - %th{ scope: 'col' } Administrateurs - %th{ scope: 'col' } Statut - %th{ scope: 'col' } Date - - @filter.procedures_result.each do |procedure| - %tbody{ 'data-controller': 'expand' } - %tr.procedure{ 'data-action': 'click->expand#toggle' } - %td - %button.fr-icon-add-line.fr-icon--sm.fr-mr-1w.fr-mb-1w.fr-text-action-high--blue-france{ 'aria-hidden': 'true', 'data-expand-target': 'icon' } - %td= procedure.libelle - %td= procedure.id - %td= procedure.administrateurs.count - %td= t procedure.aasm_state, scope: 'activerecord.attributes.procedure.aasm_state' - %td= l(procedure.published_at, format: :message_date_without_time) - %tr.hidden{ 'data-expand-target': 'content' } - %td.fr-highlight--beige-gris-galet{ colspan: '6' } - .fr-container - .fr-grid-row - .fr-col-6 - - procedure.zones.uniq.each do |zone| - = zone.label_at(procedure.published_or_created_at) - .fr-col-6 - - procedure.administrateurs.uniq.each do |admin| - = admin.email - .fr-mt-2w= paginate @filter.procedures_result, views_prefix: 'administrateurs' + - if @filter.view_admins? + .main-filter-header.fr-my-3w + .search + = link_to 'Voir la liste des démarches', all_admin_procedures_path(@filter.with_view_admins(false)), class: 'fr-btn fr-btn--secondary btn-admins' + .fr-table.fr-table--bordered + %table#all-admins + %caption + = "#{@filter.admins_result.total_count} administrateurs" + %span.hidden.fr-icon-ball-pen-fill{ 'aria-hidden': 'true', 'data-autosubmit-target': 'spinner' } + - if @filter.selected_zones.present? + .selected-zones.fr-mb-2w + - @filter.selected_zones.each do |zone| + = link_to zone.current_label, all_admin_procedures_path(@filter.without(:zone_ids, zone.id)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + - if @filter.statuses.present? + .selected-statuses.fr-mb-2w + - @filter.statuses.each do |status| + = link_to status, all_admin_procedures_path(@filter.without(:statuses, status)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + - if @filter.from_publication_date.present? + .selected-from-publication-date.fr-mb-2w + = link_to "Depuis le #{l(@filter.from_publication_date)}", all_admin_procedures_path(@filter.without(:from_publication_date)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + = paginate @filter.admins_result, views_prefix: 'administrateurs' + %thead + %tr + %th{ scope: 'col' } + %th{ scope: 'col' } Administrateurs + %th{ scope: 'col' } Nb démarches + %th{ scope: 'col' } Inscrit le + - @filter.admins_result.each do |admin| + %tbody{ 'data-controller': 'expand' } + %tr.procedure{ 'data-action': 'click->expand#toggle' } + %td + %button.fr-icon-add-line.fr-icon--sm.fr-mr-1w.fr-mb-1w.fr-text-action-high--blue-france{ 'aria-hidden': 'true', 'data-expand-target': 'icon' } + %td= admin.email + %td= admin.procedures.count + %td= l(admin.created_at, format: :message_date_without_time) + %tr.hidden{ 'data-expand-target': 'content' } + %td.fr-highlight--beige-gris-galet{ colspan: '6' } + .fr-container + .fr-col-12.fr-mr-1w + %ul + - admin.procedures.each do |procedure| + %li= procedure.libelle + .fr-mt-2w= paginate @filter.admins_result, views_prefix: 'administrateurs' + - else + .main-filter-header.fr-my-3w + .search + = link_to 'Voir la liste des administrateurs', all_admin_procedures_path(@filter.with_view_admins(true)), class: 'fr-btn fr-btn--secondary btn-admins' + .fr-table.fr-table--bordered + %table#all-demarches + %caption + = "#{@filter.procedures_result.total_count} démarches" + %span.hidden.fr-icon-ball-pen-fill{ 'aria-hidden': 'true', 'data-autosubmit-target': 'spinner' } + - if @filter.selected_zones.present? + .selected-zones.fr-mb-2w + - @filter.selected_zones.each do |zone| + = link_to zone.current_label, all_admin_procedures_path(@filter.without(:zone_ids, zone.id)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + - if @filter.statuses.present? + .selected-statuses.fr-mb-2w + - @filter.statuses.each do |status| + = link_to status, all_admin_procedures_path(@filter.without(:statuses, status)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + - if @filter.from_publication_date.present? + .selected-from-publication-date.fr-mb-2w + = link_to "Depuis #{l(@filter.from_publication_date)}", all_admin_procedures_path(@filter.without(:from_publication_date)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' + = paginate @filter.procedures_result, views_prefix: 'administrateurs' + %thead + %tr + %th{ scope: 'col' } + %th{ scope: 'col' } Démarche + %th{ scope: 'col' } N° + %th{ scope: 'col' } Administrateurs + %th{ scope: 'col' } Statut + %th{ scope: 'col' } Date + - @filter.procedures_result.each do |procedure| + %tbody{ 'data-controller': 'expand' } + %tr.procedure{ 'data-action': 'click->expand#toggle' } + %td + %button.fr-icon-add-line.fr-icon--sm.fr-mr-1w.fr-mb-1w.fr-text-action-high--blue-france{ 'aria-hidden': 'true', 'data-expand-target': 'icon' } + %td= procedure.libelle + %td= procedure.id + %td= procedure.administrateurs.count + %td= t procedure.aasm_state, scope: 'activerecord.attributes.procedure.aasm_state' + %td= l(procedure.published_at, format: :message_date_without_time) + %tr.hidden{ 'data-expand-target': 'content' } + %td.fr-highlight--beige-gris-galet{ colspan: '6' } + .fr-container + .fr-grid-row + .fr-col-6 + - procedure.zones.uniq.each do |zone| + = zone.label_at(procedure.published_or_created_at) + .fr-col-6 + - procedure.administrateurs.uniq.each do |admin| + = admin.email + .fr-mt-2w= paginate @filter.procedures_result, views_prefix: 'administrateurs' diff --git a/spec/models/procedures_filter_spec.rb b/spec/models/procedures_filter_spec.rb index bedb6c1fe..a921abab9 100644 --- a/spec/models/procedures_filter_spec.rb +++ b/spec/models/procedures_filter_spec.rb @@ -5,15 +5,26 @@ describe ProceduresFilter do context 'without filter' do let(:filters) { {} } - let!(:draft_procedure) { create(:procedure) } - let!(:published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } - let!(:closed_procedure) { create(:procedure, :closed) } + let!(:draft_procedure) { create(:procedure, administrateur: admin3) } + let!(:published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2, administrateur: admin1) } + let!(:closed_procedure) { create(:procedure, :closed, administrateur: admin2) } + let(:admin1) { create(:administrateur) } + let(:admin2) { create(:administrateur) } + let(:admin3) { create(:administrateur) } it 'returns only published and closed procedures' do expect(subject.procedures_result).to include(published_procedure) expect(subject.procedures_result).to include(closed_procedure) expect(subject.procedures_result).not_to include(draft_procedure) end + + context 'with view_admins param' do + it 'returns admins of the procedures' do + expect(subject.admins_result).to include(admin1) + expect(subject.admins_result).to include(admin2) + expect(subject.admins_result).not_to include(admin3) + end + end end context 'with zone filter' do