[#2179] Don't depend on current_gestionnaire in DossierSearchService

This commit is contained in:
Frederic Merizen 2018-07-31 12:04:49 +02:00
parent 57fd59b8d5
commit a72388bb37
2 changed files with 7 additions and 7 deletions

View file

@ -2,7 +2,7 @@ module NewGestionnaire
class RechercheController < GestionnaireController
def index
@search_terms = params[:q]
@dossiers = DossierSearchService.matching_dossiers_for_gestionnaire(@search_terms)
@dossiers = DossierSearchService.matching_dossiers_for_gestionnaire(@search_terms, current_gestionnaire)
end
end
end

View file

@ -1,9 +1,9 @@
class DossierSearchService
def self.matching_dossiers_for_gestionnaire(search_terms)
def self.matching_dossiers_for_gestionnaire(search_terms, gestionnaire)
# exact id match?
id = search_terms.to_i
if id != 0 && id_compatible?(id) # Sometimes gestionnaire is searching dossiers with a big number (ex: SIRET), ActiveRecord can't deal with them and throws ActiveModel::RangeError. id_compatible? prevents this.
dossiers = dossiers_by_id(id)
dossiers = dossiers_by_id(id, gestionnaire)
end
if dossiers.nil?
@ -13,7 +13,7 @@ class DossierSearchService
# full text search
if dossiers.empty?
dossiers = Search.new(
gestionnaire: current_gestionnaire,
gestionnaire: gestionnaire,
query: search_terms
).results
end
@ -23,9 +23,9 @@ class DossierSearchService
private
def self.dossiers_by_id(id)
dossiers = current_gestionnaire.dossiers.where(id: id) +
current_gestionnaire.dossiers_from_avis.where(id: id)
def self.dossiers_by_id(id, gestionnaire)
dossiers = gestionnaire.dossiers.where(id: id) +
gestionnaire.dossiers_from_avis.where(id: id)
dossiers.uniq
end