Merge pull request #8720 from demarches-simplifiees/feat/8549

ETQ administrateur, je souhaite filtrer avec plusieurs tags
This commit is contained in:
Colin Darie 2023-03-06 10:19:22 +00:00 committed by GitHub
commit 35674838d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 14 deletions

View file

@ -413,7 +413,7 @@ module Administrateurs
procedures_result = Procedure.select(:id).left_joins(:procedures_zones).distinct.publiees_ou_closes
procedures_result = procedures_result.where(procedures_zones: { zone_id: filter.zone_ids }) if filter.zone_ids.present?
procedures_result = procedures_result.where(aasm_state: filter.statuses) if filter.statuses.present?
procedures_result = procedures_result.where("? = ANY(tags)", filter.tag) if filter.tag.present?
procedures_result = procedures_result.where("tags @> ARRAY[?]::text[]", filter.tags) if filter.tags.present?
procedures_result = procedures_result.where('published_at >= ?', filter.from_publication_date) if filter.from_publication_date.present?
procedures_result = procedures_result.where('unaccent(libelle) ILIKE unaccent(?)', "%#{filter.libelle}%") if filter.libelle.present?
procedures_sql = procedures_result.to_sql

View file

@ -5,7 +5,7 @@ class ProceduresFilter
def initialize(admin, params)
@admin = admin
@params = params.permit(:page, :libelle, :email, :from_publication_date, :tag, zone_ids: [], statuses: [])
@params = params.permit(:page, :libelle, :email, :from_publication_date, tags: [], zone_ids: [], statuses: [])
end
def admin_zones
@ -28,8 +28,8 @@ class ProceduresFilter
params[:statuses].compact_blank if params[:statuses].present?
end
def tag
params[:tag].presence
def tags
params[:tags].compact_blank.uniq if params[:tags].present?
end
def from_publication_date
@ -72,7 +72,7 @@ class ProceduresFilter
filters << email
filters << "from-#{from_publication_date}" if from_publication_date
filters << statuses
filters << tag
filters << tags
filters.compact.join('-')
end
end

View file

@ -5,6 +5,8 @@
= hidden_field_tag 'zone_ids[]', zone_id, id: "zone_#{zone_id}"
- @filter.statuses&.each do |status|
= hidden_field_tag 'statuses[]', status, id: "status_#{status}"
- @filter.tags&.each do|tag|
= hidden_field_tag 'tags[]', tag, id: "tag_#{tag}"
= hidden_field_tag 'from_publication_date', @filter.from_publication_date if @filter.from_publication_date.present?
= f.label :libelle, 'Rechercher des démarches par libellé', class: 'fr-label'
@ -28,10 +30,11 @@
.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.tag.present?
- if @filter.tags.present?
.selected-tag.fr-mb-2w
= link_to @filter.tag, all_admin_procedures_path(@filter.without(:tag)), class: 'fr-tag fr-tag--dismiss fr-mb-1w'
- @filter.tags.each do |tag|
= link_to tag, all_admin_procedures_path(@filter.without(:tags, tag)), class: 'fr-tag fr-tag--dismiss fr-mb-1w'
- params[:tags].delete(tag)
- 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'

View file

@ -71,11 +71,13 @@
Tags
.fr-ml-1w.hidden{ 'data-expand-target': 'content' }
%div
= f.search_field :tag, placeholder: 'Choisissez un tag', list: 'tags_list', class: 'fr-input', data: { no_autosubmit: 'input', turbo_force: true }
= f.search_field :tags, placeholder: 'Choisissez un tag', list: 'tags_list', class: 'fr-input', data: { no_autosubmit: 'input', turbo_force: true }, multiple: true
%datalist#tags_list
- Procedure.tags.each do |tag|
%option{ value: tag }
- if @filter.tags.present?
- @filter.tags.each do |tag|
= f.hidden_field :tags, value: tag, multiple: true, id: "tag-#{tag.tr(' ', '_')}"
.fr-col-9
= yield(:results)
= render template: 'layouts/application'

View file

@ -176,11 +176,21 @@ describe Administrateurs::ProceduresController, type: :controller do
end
context 'with specific tag' do
let!(:tag_procedure) { create(:procedure, :published, tags: ['environnement']) }
let!(:tags_procedure) { create(:procedure, :published, tags: ['environnement', 'diplomatie']) }
it 'returns procedures with specific tag' do
get :all, params: { tag: 'environnement' }
expect(assigns(:procedures).any? { |p| p.id == tag_procedure.id }).to be_truthy
it 'returns procedure who contains at least one tag included in params' do
get :all, params: { tags: ['environnement'] }
expect(assigns(:procedures).any? { |p| p.id == tags_procedure.id }).to be_truthy
end
it 'returns procedures who contains all tags included in params' do
get :all, params: { tags: ['environnement', 'diplomatie'] }
expect(assigns(:procedures).any? { |p| p.id == tags_procedure.id }).to be_truthy
end
it 'does not returns the procedure' do
get :all, params: { tags: ['environnement', 'diplomatie', 'football'] }
expect(assigns(:procedures).any? { |p| p.id == tags_procedure.id }).to be_falsey
end
end