2017-07-31 11:58:52 +02:00
module NewGestionnaire
2017-11-22 09:56:36 +01:00
class RechercheController < GestionnaireController
2017-07-31 11:58:52 +02:00
def index
@search_terms = params [ :q ]
# exact id match?
2018-02-06 12:15:20 +01:00
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 )
2017-07-31 11:58:52 +02:00
end
2018-01-16 09:25:23 +01:00
if @dossiers . nil?
@dossiers = Dossier . none
end
2017-07-31 11:58:52 +02:00
# full text search
if @dossiers . empty?
@dossiers = Search . new (
2018-01-15 18:48:14 +01:00
gestionnaire : current_gestionnaire ,
query : @search_terms ,
page : params [ :page ]
2017-07-31 11:58:52 +02:00
) . results
end
end
2018-02-06 12:15:20 +01:00
private
def dossiers_by_id ( id )
dossiers = current_gestionnaire . dossiers . where ( id : id ) +
current_gestionnaire . dossiers_from_avis . where ( id : id )
dossiers . uniq
end
def id_compatible? ( number )
begin
ActiveRecord :: Type :: Integer . new . serialize ( number )
true
rescue ActiveModel :: RangeError
false
end
end
2017-07-31 11:58:52 +02:00
end
end