This commit is contained in:
Martin 2024-01-26 13:43:10 +01:00
parent 0e5768ec5b
commit ecc83edf4f
6 changed files with 99 additions and 53 deletions

View file

@ -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

View file

@ -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

View file

@ -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?

View file

@ -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?

View file

@ -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

View file

@ -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