From ecc83edf4fc4cb112a5e6a96a8bbf7023ddfe23a Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 26 Jan 2024 13:43:10 +0100 Subject: [PATCH] wip --- .../administrateurs/procedures_controller.rb | 4 +- .../types_de_champ_controller.rb | 4 +- app/models/procedure.rb | 34 ++---------- app/models/procedure_revision.rb | 20 +------ app/models/procedure_revision_preloader.rb | 52 +++++++++++++++++++ spec/models/procedure_revision_loader_spec.rb | 38 ++++++++++++++ 6 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 app/models/procedure_revision_preloader.rb create mode 100644 spec/models/procedure_revision_loader_spec.rb diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 89548798a..83cc74c6d 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -362,11 +362,11 @@ module Administrateurs end def champs - @procedure = Procedure.includes_for_champ_public_edition.find(@procedure.id) + ProcedureRevisionPreloader.load_one(@procedure.draft_revision) end def annotations - @procedure = Procedure.includes_for_champ_private_edition.find(@procedure.id) + ProcedureRevisionPreloader.load_one(@procedure.id) end def detail diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 00ebc9d82..f2b2b50eb 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -6,9 +6,9 @@ module Administrateurs def create type_de_champ = draft.add_type_de_champ(type_de_champ_create_params) - reload_procedure_with_includes if type_de_champ.valid? @coordinate = draft.coordinate_for(type_de_champ) + ProcedureRevisionPreloader.load_one(@coordinate.revision) @created = champ_component_from(@coordinate, focused: true) @morphed = champ_components_starting_at(@coordinate, 1) else @@ -181,7 +181,7 @@ module Administrateurs end def reload_procedure_with_includes - @procedure = Procedure.includes_for_champ_private_edition.find(@procedure.id) + ProcedureRevisionPreloader.load_one(@procedure.draft_revision) end end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 948377595..3cf8b82f9 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -237,34 +237,6 @@ class Procedure < ApplicationRecord ) } - scope :includes_for_champ_public_edition, -> { - includes(draft_revision: { - revision_types_de_champ: { - type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - }, - revision_types_de_champ_public: { - type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - }, - procedure: [] - }) - } - - scope :includes_for_champ_private_edition, -> { - includes_for_champ_public_edition.includes( - draft_revision: { - revision_types_de_champ_private: { - type_de_champ: { piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - } - } - ) - } - validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false validates :administrateurs, presence: true @@ -448,12 +420,14 @@ class Procedure < ApplicationRecord end def preload_draft_and_published_revisions + revisions = [] if !association(:published_revision).loaded? && published_revision_id.present? - association(:published_revision).target = ProcedureRevision.includes_for_compare.find(published_revision_id) + revisions.push(published_revision) end if !association(:draft_revision).loaded? && draft_revision_id.present? - association(:draft_revision).target = ProcedureRevision.includes_for_compare.find(draft_revision_id) + revisions.push(draft_revision) end + ProcedureRevisionPreloader.new(revisions).all if !revisions.empty? end def accepts_new_dossiers? diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index aac521a71..9318bcccb 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -16,25 +16,7 @@ class ProcedureRevision < ApplicationRecord has_one :published_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :published_revision_id, dependent: :nullify, inverse_of: :published_revision scope :ordered, -> { order(:created_at) } - scope :includes_for_compare, -> { - includes( - revision_types_de_champ: { - type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - }, - revision_types_de_champ_public: { - type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - }, - revision_types_de_champ_private: { - type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob, revision: [], procedure: [] }, - revision: [], - procedure: [] - } - ) - } + validate :conditions_are_valid? validate :header_sections_are_valid? validate :expressions_regulieres_are_valid? diff --git a/app/models/procedure_revision_preloader.rb b/app/models/procedure_revision_preloader.rb new file mode 100644 index 000000000..f1fb2377f --- /dev/null +++ b/app/models/procedure_revision_preloader.rb @@ -0,0 +1,52 @@ +class ProcedureRevisionPreloader + def initialize(revisions) + @revisions = revisions + end + + def all + revisions = @revisions.to_a + load_revisions(revisions) + end + + def self.load_one(revision) + ProcedureRevisionPreloader.new([revision]).all.first + end + + private + + def load_revisions(revisions) + revisions.map { load_procedure_revision_types_de_champ(_1) } + 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.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) + end + + def assign_revision_type_de_champ(revision, prtdcs) + revision.association(:revision_types_de_champ).target = prtdcs + revision.association(:types_de_champ).target = revision.revision_types_de_champ.map(&:type_de_champ) + end + + def assign_revision_type_de_champ_private(revision, prtdcs) + revision.association(:revision_types_de_champ_private).target = prtdcs.filter do + _1.parent_id.nil? && _1.type_de_champ.private? + end + revision.association(:types_de_champ_private).target = revision.revision_types_de_champ_private.map(&:type_de_champ) + end + + def assign_revision_type_de_champ_public(revision, prtdcs) + revision.association(:revision_types_de_champ_public).target = prtdcs.filter do + _1.parent_id.nil? && _1.type_de_champ.public? + end + revision.association(:types_de_champ_public).target = revision.revision_types_de_champ_public.map(&:type_de_champ) + end +end diff --git a/spec/models/procedure_revision_loader_spec.rb b/spec/models/procedure_revision_loader_spec.rb new file mode 100644 index 000000000..9dde37847 --- /dev/null +++ b/spec/models/procedure_revision_loader_spec.rb @@ -0,0 +1,38 @@ +describe ProcedureRevisionPreloader do + let(:procedure) do + create(:procedure, :published, + types_de_champ_public: [ + { type: :integer_number }, + { type: :decimal_number } + ], + types_de_champ_private: [ + { type: :text }, + { type: :textarea } + ]) + end + + describe '.load_one' do + let(:revision) { procedure.draft_revision } + 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 + 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 + + 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) + end + end +end