Merge pull request #7024 from tchak/fix-instructeur-show-n+1

fix(instructeur): avoid n+1 in instructeur_dossiers#show et user_dossiers#show
This commit is contained in:
Paul Chavard 2022-03-15 10:12:59 +01:00 committed by GitHub
commit 79bb26e4dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 32 deletions

View file

@ -43,7 +43,7 @@ module Instructeurs
end
def show
@demande_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.demande_seen_at
@demande_seen_at = current_instructeur.follows.find_by(dossier: dossier_with_champs)&.demande_seen_at
respond_to do |format|
format.pdf do
@ -254,7 +254,15 @@ module Instructeurs
@dossier ||= current_instructeur
.dossiers
.visible_by_administration
.includes(champs: :type_de_champ)
.find(params[:dossier_id])
end
def dossier_with_champs
@dossier ||= current_instructeur
.dossiers
.visible_by_administration
.with_champs
.with_annotations
.find(params[:dossier_id])
end
@ -270,19 +278,19 @@ module Instructeurs
end
def mark_demande_as_read
current_instructeur.mark_tab_as_seen(dossier, :demande)
current_instructeur.mark_tab_as_seen(@dossier, :demande)
end
def mark_messagerie_as_read
current_instructeur.mark_tab_as_seen(dossier, :messagerie)
current_instructeur.mark_tab_as_seen(@dossier, :messagerie)
end
def mark_avis_as_read
current_instructeur.mark_tab_as_seen(dossier, :avis)
current_instructeur.mark_tab_as_seen(@dossier, :avis)
end
def mark_annotations_privees_as_read
current_instructeur.mark_tab_as_seen(dossier, :annotations_privees)
current_instructeur.mark_tab_as_seen(@dossier, :annotations_privees)
end
def aasm_error_message(exception, target_state:)

View file

@ -261,32 +261,33 @@ class Dossier < ApplicationRecord
}
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }
scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) }
scope :with_champs, -> { includes(champs: :type_de_champ) }
scope :with_champs, -> {
includes(champs: [
:type_de_champ,
:geo_areas,
piece_justificative_file_attachment: :blob,
champs: [:type_de_champ, piece_justificative_file_attachment: :blob]
])
}
scope :with_annotations, -> {
includes(champs_private: [
:type_de_champ,
:geo_areas,
piece_justificative_file_attachment: :blob,
champs: [:type_de_champ, piece_justificative_file_attachment: :blob]
])
}
scope :for_api, -> {
includes(commentaires: { piece_jointe_attachment: :blob },
champs: [
:geo_areas,
:etablissement,
piece_justificative_file_attachment: :blob,
champs: [
piece_justificative_file_attachment: :blob
]
],
champs_private: [
:geo_areas,
:etablissement,
piece_justificative_file_attachment: :blob,
champs: [
piece_justificative_file_attachment: :blob
]
],
justificatif_motivation_attachment: :blob,
attestation: [],
avis: { piece_justificative_file_attachment: :blob },
traitement: [],
etablissement: [],
individual: [],
user: [])
with_champs
.with_annotations
.includes(commentaires: { piece_jointe_attachment: :blob },
justificatif_motivation_attachment: :blob,
attestation: [],
avis: { piece_justificative_file_attachment: :blob },
traitement: [],
etablissement: [],
individual: [],
user: [])
}
scope :with_notifiable_procedure, -> (opts = { notify_on_closed: false }) do
@ -1157,7 +1158,7 @@ class Dossier < ApplicationRecord
end
def geo_areas
champs.includes(:geo_areas).flat_map(&:geo_areas) + champs_private.includes(:geo_areas).flat_map(&:geo_areas)
champs.flat_map(&:geo_areas) + champs_private.flat_map(&:geo_areas)
end
def bounding_box