2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-01-26 13:43:10 +01:00
|
|
|
class ProcedureRevisionPreloader
|
|
|
|
def initialize(revisions)
|
|
|
|
@revisions = revisions
|
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
revisions = @revisions.to_a
|
|
|
|
load_revisions(revisions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_one(revision)
|
2024-02-05 11:35:01 +01:00
|
|
|
ProcedureRevisionPreloader.new([revision]).all.first # rubocop:disable Rails/RedundantActiveRecordAllMethod
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_revisions(revisions)
|
2024-01-29 10:26:09 +01:00
|
|
|
load_procedure_revision_types_de_champ(revisions)
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
def load_procedure_revision_types_de_champ(revisions)
|
|
|
|
revisions_by_id = revisions.index_by(&:id)
|
|
|
|
coordinates_by_revision_id = ProcedureRevisionTypeDeChamp
|
|
|
|
.where(revisions:)
|
2024-01-26 14:45:37 +01:00
|
|
|
.includes(type_de_champ: { notice_explicative_attachment: :blob, piece_justificative_template_attachment: :blob })
|
|
|
|
.order(:position, :id)
|
|
|
|
.to_a
|
2024-01-29 10:26:09 +01:00
|
|
|
.group_by(&:revision_id)
|
2024-01-26 14:45:37 +01:00
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
coordinates_by_revision_id.each_pair do |revision_id, coordinates|
|
|
|
|
revision = revisions_by_id[revision_id]
|
|
|
|
|
|
|
|
coordinates.each do |coordinate|
|
|
|
|
coordinate.association(:revision).target = revision
|
|
|
|
coordinate.association(:procedure).target = revision.procedure
|
|
|
|
end
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
2024-01-26 14:45:37 +01:00
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
assign_revision_type_de_champ(revisions_by_id, coordinates_by_revision_id)
|
|
|
|
assign_revision_type_de_champ_public(revisions_by_id, coordinates_by_revision_id)
|
|
|
|
assign_revision_type_de_champ_private(revisions_by_id, coordinates_by_revision_id)
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
def assign_revision_type_de_champ(revisions_by_id, coordinates_by_revision_id)
|
|
|
|
revisions_by_id.each_pair do |revision_id, revision|
|
|
|
|
revision.association(:revision_types_de_champ).target = coordinates_by_revision_id[revision_id] || []
|
|
|
|
revision.association(:types_de_champ).target = revision.revision_types_de_champ.map(&:type_de_champ)
|
|
|
|
end
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
def assign_revision_type_de_champ_private(revisions_by_id, coordinates_by_revision_id)
|
|
|
|
revisions_by_id.each_pair do |revision_id, revision|
|
|
|
|
revision.association(:revision_types_de_champ_private).target = (coordinates_by_revision_id[revision_id] || []).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)
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-29 10:26:09 +01:00
|
|
|
def assign_revision_type_de_champ_public(revisions_by_id, coordinates_by_revision_id)
|
|
|
|
revisions_by_id.each_pair do |revision_id, revision|
|
|
|
|
revision.association(:revision_types_de_champ_public).target = (coordinates_by_revision_id[revision_id] || []).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)
|
2024-01-26 13:43:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|