demarches-normaliennes/app/controllers/recherche_controller.rb

43 lines
1.3 KiB
Ruby
Raw Normal View History

2021-04-29 09:32:47 +02:00
class RechercheController < ApplicationController
before_action :authenticate_logged_user!
ITEMS_PER_PAGE = 25
PROJECTIONS = [
{ "table" => 'procedure', "column" => 'libelle' },
{ "table" => 'user', "column" => 'email' },
{ "table" => 'procedure', "column" => 'procedure_id' }
]
def index
@search_terms = search_terms
2021-05-17 15:42:21 +02:00
@instructeur_dossiers_ids = DossierSearchService
.matching_dossiers(current_instructeur&.dossiers, @search_terms, with_annotation: true)
2021-05-17 15:42:21 +02:00
expert_dossier_ids = DossierSearchService
.matching_dossiers(current_expert&.dossiers, @search_terms)
2021-04-29 09:32:47 +02:00
matching_dossiers_ids = (@instructeur_dossiers_ids + expert_dossier_ids).uniq
2021-05-17 15:42:21 +02:00
2021-04-29 09:32:47 +02:00
@paginated_ids = Kaminari
.paginate_array(matching_dossiers_ids)
2021-04-29 09:32:47 +02:00
.page(page)
.per(ITEMS_PER_PAGE)
@projected_dossiers = DossierProjectionService.project(@paginated_ids, PROJECTIONS)
2021-05-17 15:42:21 +02:00
@dossiers_count = matching_dossiers_ids.count
2021-05-17 15:42:21 +02:00
@followed_dossiers_id = current_instructeur&.followed_dossiers&.where(id: @paginated_ids)&.ids || []
@dossier_avis_ids_h = current_expert&.avis&.where(dossier_id: @paginated_ids)&.pluck(:dossier_id, :id).to_h || {}
2021-04-29 09:32:47 +02:00
end
private
def page
params[:page].presence || 1
end
def search_terms
params[:q]
end
end