perf(ProcedureRevisionPreloader): preload revision quickly

This commit is contained in:
Martin 2024-01-26 14:45:37 +01:00
parent ecc83edf4f
commit 01752bc2e8
5 changed files with 29 additions and 16 deletions

View file

@ -366,7 +366,7 @@ module Administrateurs
end
def annotations
ProcedureRevisionPreloader.load_one(@procedure.id)
ProcedureRevisionPreloader.load_one(@procedure.draft_revision)
end
def detail

View file

@ -110,7 +110,7 @@ module Administrateurs
flash.alert = errors
else
@coordinate = draft.remove_type_de_champ(params[:stable_id])
@coordinate.association(:revision).target = ProcedureRevision.includes_for_compare.find(coordinate.revision.id)
ProcedureRevisionPreloader.load_one(@coordinate.revision)
if @coordinate.present?
@destroyed = @coordinate
@morphed = champ_components_starting_at(@coordinate)
@ -181,7 +181,7 @@ module Administrateurs
end
def reload_procedure_with_includes
ProcedureRevisionPreloader.load_one(@procedure.draft_revision)
ProcedureRevisionPreloader.load_one(draft)
end
end
end

View file

@ -19,13 +19,17 @@ class ProcedureRevisionPreloader
end
def load_procedure_revision_types_de_champ(revision)
prtdcs = ProcedureRevisionTypeDeChamp.where(revision:).includes({
type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }
}).to_a
prtdcs = ProcedureRevisionTypeDeChamp
.where(revision:)
.includes(type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob })
.order(:position, :id)
.to_a
prtdcs.each do |prtdc|
prtdc.association(:revision).target = revision
prtdc.association(:procedure).target = revision.procedure
end
assign_revision_type_de_champ(revision, prtdcs)
assign_revision_type_de_champ_public(revision, prtdcs)
assign_revision_type_de_champ_private(revision, prtdcs)

View file

@ -31,7 +31,7 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
def siblings
if parent_id.present?
revision.revision_types_de_champ.where(parent_id: parent_id).ordered
revision.revision_types_de_champ.filter { _1.parent_id == parent_id }
elsif private?
revision.revision_types_de_champ_private
else

View file

@ -16,23 +16,32 @@ describe ProcedureRevisionPreloader do
subject { ProcedureRevisionPreloader.load_one(revision) }
it 'assigns stuffs correctly' do
original_procedure = Procedure.find(procedure.id)
expect(procedure.draft_revision.association(:revision_types_de_champ).loaded?).to be_falsy
expect(procedure.draft_revision.association(:revision_types_de_champ_public).loaded?).to be_falsy
expect(procedure.draft_revision.association(:revision_types_de_champ_private).loaded?).to be_falsy
expect(procedure.draft_revision.association(:types_de_champ).loaded?).to be_falsy
expect(procedure.draft_revision.association(:types_de_champ_public).loaded?).to be_falsy
expect(procedure.draft_revision.association(:types_de_champ_private).loaded?).to be_falsy
subject
expect(procedure.draft_revision.association(:revision_types_de_champ).loaded?).to be_truthy
# check it changes loaded from false to true
expect { subject }.to change { procedure.draft_revision.association(:revision_types_de_champ).loaded? }.from(false).to(true)
# check loaded relationships
expect(procedure.draft_revision.association(:revision_types_de_champ_public).loaded?).to be_truthy
expect(procedure.draft_revision.association(:revision_types_de_champ_private).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ_public).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ_private).loaded?).to be_truthy
# check nested relationship
expect(revision.revision_types_de_champ.first.association(:revision).loaded?).to eq(true)
expect(revision.revision_types_de_champ.first.association(:procedure).loaded?).to eq(true)
# check order
original = Procedure.find(procedure.id)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ_public)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ_private)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ_public)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ_private)
end
def expect_relation_is_preloaded_sorted(original, preloaded, association)
expect(original.draft_revision.send(association).map(&:id)).to eq(preloaded.draft_revision.send(association).map(&:id))
end
end
end