[#3477] Move eager_load_displayed_fields to model

This commit is contained in:
Frederic Merizen 2019-02-19 16:57:45 +01:00
parent c81adb80fd
commit 393f1b1971
2 changed files with 32 additions and 32 deletions

View file

@ -87,7 +87,7 @@ module NewGestionnaire
@dossiers = @dossiers.where(id: filtered_sorted_paginated_ids)
@dossiers = eager_load_displayed_fields(@dossiers)
@dossiers = procedure_presentation.eager_load_displayed_fields(@dossiers)
@dossiers = @dossiers.sort_by { |d| filtered_sorted_paginated_ids.index(d.id) }
@ -241,37 +241,6 @@ module NewGestionnaire
procedure_presentation.fields_for_select
end
def eager_load_displayed_fields(dossiers)
procedure_presentation.displayed_fields
.reject { |field| field['table'] == 'self' }
.group_by do |field|
case field['table']
when 'type_de_champ', 'type_de_champ_private'
'type_de_champ_group'
else
field['table']
end
end.reduce(dossiers) do |dossiers, (group_key, fields)|
if group_key != 'type_de_champ_group'
dossiers.includes(fields.first['table'])
else
if fields.any? { |field| field['table'] == 'type_de_champ' }
dossiers = dossiers.includes(:champs).references(:champs)
end
if fields.any? { |field| field['table'] == 'type_de_champ_private' }
dossiers = dossiers.includes(:champs_private).references(:champs_private)
end
where_conditions = fields.map do |field|
"champs.type_de_champ_id = #{field['column']}"
end.join(" OR ")
dossiers.where(where_conditions)
end
end
end
def kaminarize(current_page, total)
@dossiers.instance_eval <<-EVAL
def current_page

View file

@ -146,6 +146,37 @@ class ProcedurePresentation < ApplicationRecord
end.reduce(:&)
end
def eager_load_displayed_fields(dossiers)
displayed_fields
.reject { |field| field['table'] == 'self' }
.group_by do |field|
case field['table']
when 'type_de_champ', 'type_de_champ_private'
'type_de_champ_group'
else
field['table']
end
end.reduce(dossiers) do |dossiers, (group_key, fields)|
if group_key != 'type_de_champ_group'
dossiers.includes(fields.first['table'])
else
if fields.any? { |field| field['table'] == 'type_de_champ' }
dossiers = dossiers.includes(:champs).references(:champs)
end
if fields.any? { |field| field['table'] == 'type_de_champ_private' }
dossiers = dossiers.includes(:champs_private).references(:champs_private)
end
where_conditions = fields.map do |field|
"champs.type_de_champ_id = #{field['column']}"
end.join(" OR ")
dossiers.where(where_conditions)
end
end
end
private
class Filter