perf(preloader): fix N+1 on non mandatory repetition blocks

We have to tell repetition.champs association has been loaded
even when it's empty.
This commit is contained in:
Colin Darie 2023-11-30 14:10:33 +01:00
parent a330118929
commit 42ea9ed2f3
2 changed files with 9 additions and 2 deletions

View file

@ -97,7 +97,10 @@ class DossierPreloader
end
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|
champ.association(:dossier).target = dossier

View file

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