demarches-normaliennes/app/controllers/new_gestionnaire/recherche_controller.rb

43 lines
1.1 KiB
Ruby
Raw Normal View History

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?
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(
gestionnaire: current_gestionnaire,
2018-07-31 11:47:13 +02:00
query: @search_terms
2017-07-31 11:58:52 +02:00
).results
end
end
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