refactor(correction): preload corrections for exports

This commit is contained in:
Colin Darie 2023-06-28 18:57:32 +02:00
parent 6b908ffb27
commit c95f4ab700
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 8 additions and 4 deletions

View file

@ -4,6 +4,7 @@ module DossierCorrectableConcern
included do
A_CORRIGER = 'a_corriger'
has_many :corrections, class_name: 'DossierCorrection', dependent: :destroy
has_many :pending_corrections, -> { DossierCorrection.pending }, class_name: 'DossierCorrection', inverse_of: :dossier
scope :with_pending_corrections, -> { joins(:corrections).where(corrections: { resolved_at: nil }) }
@ -18,7 +19,7 @@ module DossierCorrectableConcern
end
def may_flag_as_pending_correction?
return false if corrections.pending.exists?
return false if pending_corrections.exists?
en_construction? || may_repasser_en_construction?
end
@ -27,15 +28,17 @@ module DossierCorrectableConcern
# We don't want to show any alert if user is not allowed to modify the dossier
return false unless en_construction?
corrections.pending.exists?
return pending_corrections.any? if pending_corrections.loaded?
pending_corrections.exists?
end
def pending_correction
corrections.pending.first
pending_corrections.first
end
def resolve_pending_correction!
corrections.pending.update!(resolved_at: Time.current)
pending_corrections.update!(resolved_at: Time.current)
end
end
end

View file

@ -481,6 +481,7 @@ class Dossier < ApplicationRecord
:traitement,
:groupe_instructeur,
:etablissement,
:pending_corrections,
procedure: [:groupe_instructeurs],
avis: [:claimant, :expert]
).ordered_for_export).in_batches