2018-07-31 12:03:01 +02:00
class DossierSearchService
2019-08-06 11:02:54 +02:00
def self . matching_dossiers_for_instructeur ( search_terms , instructeur )
dossier_by_exact_id_for_instructeur ( search_terms , instructeur )
. presence || dossier_by_full_text_for_instructeur ( search_terms , instructeur )
2018-07-31 12:03:01 +02:00
end
private
2019-08-06 11:02:54 +02:00
def self . dossier_by_exact_id_for_instructeur ( search_terms , instructeur )
2018-07-31 14:29:37 +02:00
id = search_terms . to_i
2019-08-06 11:02:54 +02:00
if id != 0 && id_compatible? ( id ) # Sometimes instructeur 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 , instructeur )
2018-07-31 14:54:55 +02:00
else
Dossier . none
2018-07-31 14:29:37 +02:00
end
end
2019-08-06 11:02:54 +02:00
def self . dossiers_by_id ( id , instructeur )
( instructeur . dossiers . where ( id : id ) + instructeur . dossiers_from_avis . where ( id : id ) ) . uniq
2018-07-31 12:03:01 +02:00
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
2018-07-31 12:03:01 +02:00
end
2018-07-31 14:32:17 +02:00
2019-08-06 11:02:54 +02:00
def self . dossier_by_full_text_for_instructeur ( search_terms , instructeur )
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 ) ) } ) "
2019-08-06 11:02:54 +02:00
instructeur
2018-07-31 15:08:10 +02:00
. dossiers
. 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 )
2019-05-22 14:34:46 +02:00
( search_terms || " " )
. strip
2019-01-22 17:16:49 +01:00
. gsub ( / ['? \\ :&|!<> \ ( \ )] / , " " ) # drop disallowed characters
2018-08-22 18:36:24 +02:00
. split ( / \ s+ / ) # split words
. map { | x | " #{ x } :* " } # enable prefix matching
2018-07-31 15:08:10 +02:00
. join ( " & " )
2018-07-31 14:32:17 +02:00
end
2018-07-31 12:03:01 +02:00
end