[#2579] Simplify sorted_ids

This commit is contained in:
Frederic Merizen 2018-09-25 11:35:49 +02:00
parent 3bb6fd2d47
commit 314e9354c1

View file

@ -102,39 +102,39 @@ class DossierFieldService
table = procedure_presentation.sort['table'] table = procedure_presentation.sort['table']
column = procedure_presentation.sort['column'] column = procedure_presentation.sort['column']
order = procedure_presentation.sort['order'] order = procedure_presentation.sort['order']
includes = ''
where = ''
sorted_ids = nil
case table case table
when 'notifications' when 'notifications'
procedure = procedure_presentation.assign_to.procedure procedure = procedure_presentation.assign_to.procedure
dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure) dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure)
if order == 'desc' if order == 'desc'
sorted_ids = dossiers_id_with_notification + (dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification) return dossiers_id_with_notification +
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
else else
sorted_ids = (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification return (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
dossiers_id_with_notification
end end
when 'self' when 'self'
order = "dossiers.#{column} #{order}" return dossiers
.order("dossiers.#{column} #{order}")
.pluck(:id)
when 'france_connect_information' when 'france_connect_information'
includes = { user: :france_connect_information } return dossiers
order = "france_connect_informations.#{column} #{order}" .includes(user: :france_connect_information)
.order("france_connect_informations.#{column} #{order}")
.pluck(:id)
when 'type_de_champ', 'type_de_champ_private' when 'type_de_champ', 'type_de_champ_private'
includes = table == 'type_de_champ' ? :champs : :champs_private return dossiers
where = "champs.type_de_champ_id = #{column.to_i}" .includes(table == 'type_de_champ' ? :champs : :champs_private)
order = "champs.value #{order}" .where("champs.type_de_champ_id = #{column.to_i}")
.order("champs.value #{order}")
.pluck(:id)
else else
includes = table return dossiers
order = "#{table.pluralize}.#{column} #{order}" .includes(table)
.order("#{table.pluralize}.#{column} #{order}")
.pluck(:id)
end end
if sorted_ids.nil?
sorted_ids = dossiers.includes(includes).where(where).order(order).pluck(:id)
end
sorted_ids
end end
private private