[#3477] Clarify what the previous code of eager_load_displayed_fields does
This commit is contained in:
parent
f0d83b1de8
commit
cf3f2409dd
1 changed files with 24 additions and 21 deletions
|
@ -138,29 +138,32 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def eager_load_displayed_fields(dossiers)
|
def eager_load_displayed_fields(dossiers)
|
||||||
displayed_fields
|
fields_to_eager_load = displayed_fields.reject { |field| field['table'] == 'self' }
|
||||||
.reject { |field| field['table'] == 'self' }
|
|
||||||
.group_by do |field|
|
relations_to_include = fields_to_eager_load
|
||||||
case field['table']
|
.pluck('table')
|
||||||
when 'type_de_champ', 'type_de_champ_private'
|
.map do |table|
|
||||||
'type_de_champ_group'
|
case table
|
||||||
|
when 'type_de_champ'
|
||||||
|
:champs
|
||||||
|
when 'type_de_champ_private'
|
||||||
|
:champs_private
|
||||||
else
|
else
|
||||||
field['table']
|
table
|
||||||
end
|
end
|
||||||
end.reduce(dossiers) do |dossiers, (group_key, fields)|
|
end
|
||||||
if group_key != 'type_de_champ_group'
|
.uniq
|
||||||
dossiers.includes(fields.first['table'])
|
|
||||||
else
|
champ_fields = fields_to_eager_load.select do |field|
|
||||||
if fields.any? { |field| field['table'] == 'type_de_champ' }
|
['type_de_champ', 'type_de_champ_private'].include?(field['table'])
|
||||||
dossiers = dossiers.includes(:champs).references(:champs)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if fields.any? { |field| field['table'] == 'type_de_champ_private' }
|
if champ_fields.present?
|
||||||
dossiers = dossiers.includes(:champs_private).references(:champs_private)
|
dossiers
|
||||||
end
|
.includes(relations_to_include)
|
||||||
|
.where(champs: { type_de_champ_id: champ_fields.pluck('column') })
|
||||||
dossiers.where(champs: { type_de_champ_id: fields.pluck('column') })
|
else
|
||||||
end
|
dossiers.includes(relations_to_include)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue