[#2179] Extract dossiers_by_exact_id_for_gestionnaire method

This commit is contained in:
Frederic Merizen 2018-07-31 14:29:37 +02:00
parent a72388bb37
commit d734f978a3

View file

@ -1,14 +1,7 @@
class DossierSearchService
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, gestionnaire)
end
if dossiers.nil?
dossiers = Dossier.none
end
dossiers = dossier_by_exact_id_for_gestionnaire(search_terms, gestionnaire)
# full text search
if dossiers.empty?
@ -23,6 +16,17 @@ class DossierSearchService
private
def self.dossier_by_exact_id_for_gestionnaire(search_terms, gestionnaire)
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, gestionnaire)
end
if dossiers.nil?
dossiers = Dossier.none
end
end
def self.dossiers_by_id(id, gestionnaire)
dossiers = gestionnaire.dossiers.where(id: id) +
gestionnaire.dossiers_from_avis.where(id: id)