Merge pull request #6022 from tchak/fix-export-n+1

Instructeur : amélioration des performances de l'export des dossiers (#6022)
This commit is contained in:
Pierre de La Morinerie 2021-03-25 19:49:23 +01:00 committed by GitHub
commit 9b9e744fcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 33 deletions

View file

@ -175,8 +175,16 @@ class Dossier < ApplicationRecord
:user, :user,
:individual, :individual,
:followers_instructeurs, :followers_instructeurs,
:avis,
:traitements, :traitements,
:groupe_instructeur,
procedure: [
:groupe_instructeurs,
:draft_types_de_champ,
:draft_types_de_champ_private,
:published_types_de_champ,
:published_types_de_champ_private
],
avis: [:claimant, :expert],
etablissement: :champ, etablissement: :champ,
champs: { champs: {
type_de_champ: [], type_de_champ: [],
@ -195,8 +203,7 @@ class Dossier < ApplicationRecord
type_de_champ: [], type_de_champ: [],
piece_justificative_file_attachment: :blob piece_justificative_file_attachment: :blob
] ]
}, }
procedure: :groupe_instructeurs
).order(en_construction_at: 'asc') ).order(en_construction_at: 'asc')
} }
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }

View file

@ -66,9 +66,6 @@ class Procedure < ApplicationRecord
has_many :draft_types_de_champ, through: :draft_revision, source: :types_de_champ has_many :draft_types_de_champ, through: :draft_revision, source: :types_de_champ
has_many :draft_types_de_champ_private, through: :draft_revision, source: :types_de_champ_private has_many :draft_types_de_champ_private, through: :draft_revision, source: :types_de_champ_private
has_many :all_types_de_champ, -> { joins(:procedure).where('types_de_champ.revision_id != procedures.draft_revision_id') }, through: :revisions, source: :types_de_champ
has_many :all_types_de_champ_private, -> { joins(:procedure).where('types_de_champ.revision_id != procedures.draft_revision_id') }, through: :revisions, source: :types_de_champ_private
has_many :experts_procedures, dependent: :destroy has_many :experts_procedures, dependent: :destroy
has_many :experts, through: :experts_procedures has_many :experts, through: :experts_procedures
@ -92,31 +89,11 @@ class Procedure < ApplicationRecord
end end
def types_de_champ_for_export def types_de_champ_for_export
if brouillon? types_de_champ.reject(&:exclude_from_export?)
draft_types_de_champ.reject(&:exclude_from_export?)
else
all_types_de_champ
.uniq
.reject(&:exclude_from_export?)
.filter(&:active_revision?)
.group_by(&:stable_id).values.map do |types_de_champ|
types_de_champ.sort_by(&:created_at).last
end
end
end end
def types_de_champ_private_for_export def types_de_champ_private_for_export
if brouillon? types_de_champ_private.reject(&:exclude_from_export?)
draft_types_de_champ_private
else
all_types_de_champ_private
.uniq
.reject(&:exclude_from_export?)
.filter(&:active_revision?)
.group_by(&:stable_id).values.map do |types_de_champ|
types_de_champ.sort_by(&:created_at).last
end
end
end end
has_many :administrateurs_procedures has_many :administrateurs_procedures
@ -596,7 +573,7 @@ class Procedure < ApplicationRecord
end end
def routee? def routee?
groupe_instructeurs.count > 1 groupe_instructeurs.size > 1
end end
def defaut_groupe_instructeur_for_new_dossier def defaut_groupe_instructeur_for_new_dossier

View file

@ -206,10 +206,6 @@ class TypeDeChamp < ApplicationRecord
!private? !private?
end end
def active_revision?
revisions.include?(procedure.active_revision)
end
def self.type_champ_to_class_name(type_champ) def self.type_champ_to_class_name(type_champ)
"TypesDeChamp::#{type_champ.classify}TypeDeChamp" "TypesDeChamp::#{type_champ.classify}TypeDeChamp"
end end