demarches-normaliennes/app/services/dossier_search_service.rb

48 lines
1.6 KiB
Ruby
Raw Normal View History

class DossierSearchService
def self.matching_dossiers_for_gestionnaire(search_terms, gestionnaire)
dossier_by_exact_id_for_gestionnaire(search_terms, gestionnaire)
.presence || dossier_by_full_text_for_gestionnaire(search_terms, gestionnaire)
end
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_by_id(id, gestionnaire)
else
Dossier.none
end
end
def self.dossiers_by_id(id, gestionnaire)
2018-07-31 14:53:22 +02:00
(gestionnaire.dossiers.where(id: id) + gestionnaire.dossiers_from_avis.where(id: id)).uniq
end
def self.id_compatible?(number)
2018-07-31 14:45:50 +02:00
ActiveRecord::Type::Integer.new.serialize(number)
true
rescue ActiveModel::RangeError
false
end
def self.dossier_by_full_text_for_gestionnaire(search_terms, gestionnaire)
2018-07-31 15:08:10 +02:00
ts_vector = "to_tsvector('french', search_terms || private_search_terms)"
ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})"
gestionnaire
.dossiers
.not_archived
.state_not_brouillon
.where("#{ts_vector} @@ #{ts_query}")
.order("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC")
end
def self.to_tsquery(search_terms)
search_terms.gsub(/['?\\:&|!]/, "") # drop disallowed characters
.split(/\s+/) # split words
.map { |x| "#{x}:*" } # enable prefix matching
.join(" & ")
end
end