diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 0e8aee571..6a35c42d9 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -230,25 +230,7 @@ class Dossier < ApplicationRecord :published_types_de_champ_private ], avis: [:claimant, :expert], - etablissement: :champ, - champs: { - type_de_champ: [], - etablissement: :champ, - piece_justificative_file_attachment: :blob, - champs: [ - type_de_champ: [], - piece_justificative_file_attachment: :blob - ] - }, - champs_private: { - type_de_champ: [], - etablissement: :champ, - piece_justificative_file_attachment: :blob, - champs: [ - type_de_champ: [], - piece_justificative_file_attachment: :blob - ] - } + etablissement: :champ ).order(en_construction_at: 'asc') } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } @@ -387,6 +369,41 @@ class Dossier < ApplicationRecord validates :individual, presence: true, if: -> { revision.procedure.for_individual? } validates :groupe_instructeur, presence: true, if: -> { !brouillon? } + EXPORT_BATCH_SIZE = 5000 + + def self.downloadable_sorted_batch + dossiers = downloadable_sorted.to_a + (dossiers.size.to_f / EXPORT_BATCH_SIZE).ceil.times do |i| + start_index = i * EXPORT_BATCH_SIZE + end_index = start_index + EXPORT_BATCH_SIZE - 1 + load_champs(dossiers[start_index..end_index]) + end + dossiers + end + + def self.load_champs(dossiers) + ::ActiveRecord::Associations::Preloader.new.preload(dossiers, { + champs: { + type_de_champ: [], + etablissement: :champ, + piece_justificative_file_attachment: :blob, + champs: [ + type_de_champ: [], + piece_justificative_file_attachment: :blob + ] + }, + champs_private: { + type_de_champ: [], + etablissement: :champ, + piece_justificative_file_attachment: :blob, + champs: [ + type_de_champ: [], + piece_justificative_file_attachment: :blob + ] + } + }) + end + def user_deleted? persisted? && user_id.nil? end diff --git a/app/services/procedure_export_service.rb b/app/services/procedure_export_service.rb index d6c8ed69d..2de0cf91c 100644 --- a/app/services/procedure_export_service.rb +++ b/app/services/procedure_export_service.rb @@ -3,7 +3,7 @@ class ProcedureExportService def initialize(procedure, dossiers) @procedure = procedure - @dossiers = dossiers.downloadable_sorted + @dossiers = dossiers.downloadable_sorted_batch @tables = [:dossiers, :etablissements, :avis] + champs_repetables_options end