search procedures by libelle

This commit is contained in:
Christophe Robillard 2022-11-16 11:03:23 +01:00
parent b29d0fc346
commit 8e95c6a2ba
4 changed files with 32 additions and 3 deletions

View file

@ -350,6 +350,7 @@ module Administrateurs
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('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_result
end

View file

@ -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, :libelle, :from_publication_date, zone_ids: [], statuses: [])
end
def admin_zones
@ -36,6 +36,10 @@ class ProceduresFilter
nil
end
def libelle
ActiveRecord::Base.sanitize_sql_like(params[:libelle]) if params[:libelle].present?
end
def zone_filtered?(zone_id)
zone_ids&.map(&:to_i)&.include?(zone_id)
end

View file

@ -1,12 +1,23 @@
- content_for :results do
.main-filter-header.fr-my-3w
.search
= link_to 'Voir la liste des administrateurs', administrateurs_admin_procedures_path(@filter.params), class: 'fr-btn fr-btn--secondary btn-admins'
= form_with(url: all_admin_procedures_path, method: :get, html: {'data-autosubmit-target': 'form', role: 'search' }) do |f|
- @filter.zone_ids&.each do |zone_id|
= hidden_field_tag 'zone_ids[]', zone_id
- @filter.statuses&.each do |status|
= hidden_field_tag 'statuses[]', status
= 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'
= f.search_field 'libelle', size: 40, class: 'fr-input'
.switch= link_to 'Voir la liste des administrateurs', administrateurs_admin_procedures_path(@filter.params), class: 'fr-btn fr-btn--secondary'
.fr-table.fr-table--bordered
%table#all-demarches
%caption
= "#{@procedures.total_count} démarches"
%span.hidden.fr-icon-ball-pen-fill{ 'aria-hidden': 'true', 'data-autosubmit-target': 'spinner' }
- if @filter.libelle
.selected-query.fr-mb-2w
= link_to @filter.libelle, all_admin_procedures_path(@filter.without(:libelle)), class: 'fr-tag fr-tag--dismiss fr-mb-1w'
- if @filter.selected_zones.present?
.selected-zones.fr-mb-2w
- @filter.selected_zones.each do |zone|

View file

@ -151,6 +151,19 @@ describe Administrateurs::ProceduresController, type: :controller do
expect(assigns(:procedures)).not_to include(procedure3)
end
end
context 'with libelle search' do
let!(:procedure1) { create(:procedure, :published, libelle: 'Demande de subvention') }
let!(:procedure2) { create(:procedure, :published, libelle: "Fonds d'aide public « Prime Entrepreneurs des Quartiers »") }
let!(:procedure3) { create(:procedure, :published, libelle: "Hackaton pour entrepreneurs en résidence") }
it 'returns procedures with specific terms in libelle' do
get :all, params: { libelle: 'entrepreneur' }
expect(assigns(:procedures)).to include(procedure2)
expect(assigns(:procedures)).to include(procedure3)
expect(assigns(:procedures)).not_to include(procedure1)
end
end
end
describe 'GET #administrateurs' do