From 0ba3515d41ca1d3a6f728745dd1bef341cc8b306 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 27 Feb 2019 12:12:34 +0100 Subject: [PATCH] [#3477] Filter by 'column ilike values' as a scope --- .../concerns/dossier_filtering_concern.rb | 6 ++++ app/models/procedure_presentation.rb | 35 ++++++------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/models/concerns/dossier_filtering_concern.rb b/app/models/concerns/dossier_filtering_concern.rb index 8990a60b8..58bcff54b 100644 --- a/app/models/concerns/dossier_filtering_concern.rb +++ b/app/models/concerns/dossier_filtering_concern.rb @@ -11,5 +11,11 @@ module DossierFilteringConcern none end } + + scope :filter_ilike, lambda { |table, column, values| + table_column = ProcedurePresentation.sanitized_column(table, column) + q = Array.new(values.count, "(#{table_column} ILIKE ?)").join(' OR ') + where(q, *(values.map { |value| "%#{value}%" })) + } end end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 37dbc9ede..1ba45bc68 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -112,11 +112,10 @@ class ProcedurePresentation < ApplicationRecord dossiers.filter_by_datetimes(column, dates) when 'type_de_champ', 'type_de_champ_private' relation = table == 'type_de_champ' ? :champs : :champs_private - Filter.new( - dossiers - .includes(relation) - .where("champs.type_de_champ_id = ?", column.to_i) - ).where_ilike(:champ, :value, values) + dossiers + .includes(relation) + .where("champs.type_de_champ_id = ?", column.to_i) + .filter_ilike(:champ, :value, values) when 'etablissement' if column == 'entreprise_date_creation' dates = values.map { |v| v.to_date rescue nil } @@ -124,16 +123,14 @@ class ProcedurePresentation < ApplicationRecord .includes(table) .where(table.pluralize => { column => dates }) else - Filter.new( - dossiers - .includes(table) - ).where_ilike(table, column, values) - end - when 'user', 'individual' - Filter.new( dossiers .includes(table) - ).where_ilike(table, column, values) + .filter_ilike(table, column, values) + end + when 'user', 'individual' + dossiers + .includes(table) + .filter_ilike(table, column, values) end.pluck(:id) end.reduce(:&) end @@ -167,18 +164,6 @@ class ProcedurePresentation < ApplicationRecord private - class Filter - def initialize(dossiers) - @dossiers = dossiers - end - - def where_ilike(table, column, values) - table_column = ProcedurePresentation.sanitized_column(table, column) - q = Array.new(values.count, "(#{table_column} ILIKE ?)").join(' OR ') - @dossiers.where(q, *(values.map { |value| "%#{value}%" })) - end - end - def check_allowed_displayed_fields displayed_fields.each do |field| table = field['table']