Merge pull request #9771 from colinux/fix-preloader-repetitions

Perf chargement de dossier: fix N+1 sur les champs répétitions non obligatoires
This commit is contained in:
Paul Chavard 2023-12-01 10:43:17 +00:00 committed by GitHub
commit 0f43e8bbf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View file

@ -97,7 +97,10 @@ class DossierPreloader
end end
def load_champs(parent, name, champs, dossier, children_by_parent) def load_champs(parent, name, champs, dossier, children_by_parent)
return if champs.empty? if champs.empty?
parent.association(name).target = [] # tells to Rails association has been loaded
return
end
champs.each do |champ| champs.each do |champ|
champ.association(:dossier).target = dossier champ.association(:dossier).target = dossier

View file

@ -2,12 +2,14 @@ describe DossierPreloader do
let(:types_de_champ) do let(:types_de_champ) do
[ [
{ type: :text }, { type: :text },
{ type: :repetition, mandatory: true, children: [{ type: :text }] } { type: :repetition, mandatory: true, children: [{ type: :text }] },
{ type: :repetition, mandatory: false, children: [{ type: :text }] }
] ]
end end
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:repetition) { subject.champs_public.second } let(:repetition) { subject.champs_public.second }
let(:repetition_optional) { subject.champs_public.third }
let(:first_child) { subject.champs_public.second.champs.first } let(:first_child) { subject.champs_public.second.champs.first }
describe 'all' do describe 'all' do
@ -37,6 +39,8 @@ describe DossierPreloader do
expect(subject.champs_public.first.conditional?).to eq(false) expect(subject.champs_public.first.conditional?).to eq(false)
expect(first_child.parent).to eq(repetition) expect(first_child.parent).to eq(repetition)
expect(repetition.champs.first).to eq(first_child)
expect(repetition_optional.champs).to be_empty
end end
expect(count).to eq(0) expect(count).to eq(0)