diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 83cc74c6d..22ee5a4a0 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -366,7 +366,7 @@ module Administrateurs end def annotations - ProcedureRevisionPreloader.load_one(@procedure.id) + ProcedureRevisionPreloader.load_one(@procedure.draft_revision) end def detail diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index f2b2b50eb..d357e6474 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -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 diff --git a/app/models/procedure_revision_preloader.rb b/app/models/procedure_revision_preloader.rb index f1fb2377f..c40ee9544 100644 --- a/app/models/procedure_revision_preloader.rb +++ b/app/models/procedure_revision_preloader.rb @@ -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) diff --git a/app/models/procedure_revision_type_de_champ.rb b/app/models/procedure_revision_type_de_champ.rb index c4842da20..2c27f26bf 100644 --- a/app/models/procedure_revision_type_de_champ.rb +++ b/app/models/procedure_revision_type_de_champ.rb @@ -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 diff --git a/spec/models/procedure_revision_loader_spec.rb b/spec/models/procedure_revision_preloader_spec.rb similarity index 53% rename from spec/models/procedure_revision_loader_spec.rb rename to spec/models/procedure_revision_preloader_spec.rb index 9dde37847..b5e6c97f4 100644 --- a/spec/models/procedure_revision_loader_spec.rb +++ b/spec/models/procedure_revision_preloader_spec.rb @@ -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