Revert "Merge pull request #10771 from tchak/refactor-champs-revert"
This reverts commitc902061ebf
, reversing changes made tob4ed11c788
.
This commit is contained in:
parent
6b322d61ca
commit
10a1ae5534
35 changed files with 189 additions and 205 deletions
|
@ -87,13 +87,13 @@ class AttestationTemplate < ApplicationRecord
|
|||
end
|
||||
|
||||
def unspecified_champs_for_dossier(dossier)
|
||||
types_de_champ_by_tag_id = dossier.revision.types_de_champ.index_by { "tdc#{_1.stable_id}" }
|
||||
champs_by_stable_id = dossier.champs_for_revision(root: true).index_by { "tdc#{_1.stable_id}" }
|
||||
|
||||
used_tags.filter_map do |used_tag|
|
||||
corresponding_type_de_champ = types_de_champ_by_tag_id[used_tag]
|
||||
corresponding_champ = champs_by_stable_id[used_tag]
|
||||
|
||||
if corresponding_type_de_champ && dossier.project_champ(corresponding_type_de_champ, nil).blank?
|
||||
corresponding_type_de_champ
|
||||
if corresponding_champ && corresponding_champ.blank?
|
||||
corresponding_champ
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,8 +6,6 @@ class Champ < ApplicationRecord
|
|||
|
||||
self.ignored_columns += [:type_de_champ_id]
|
||||
|
||||
attr_readonly :stable_id
|
||||
|
||||
belongs_to :dossier, inverse_of: false, touch: true, optional: false
|
||||
belongs_to :parent, class_name: 'Champ', optional: true
|
||||
has_many_attached :piece_justificative_file
|
||||
|
@ -95,7 +93,7 @@ class Champ < ApplicationRecord
|
|||
end
|
||||
|
||||
def child?
|
||||
row_id.present?
|
||||
parent_id.present?
|
||||
end
|
||||
|
||||
# used for the `required` html attribute
|
||||
|
|
|
@ -37,7 +37,7 @@ class Champs::RepetitionChamp < Champ
|
|||
end
|
||||
|
||||
def blank?
|
||||
row_ids.empty?
|
||||
champs.empty?
|
||||
end
|
||||
|
||||
def search_terms
|
||||
|
|
|
@ -16,9 +16,9 @@ module ChampsValidateConcern
|
|||
def validate_champ_value?
|
||||
case validation_context
|
||||
when :champs_public_value
|
||||
public? && in_dossier_revision? && visible?
|
||||
public? && visible?
|
||||
when :champs_private_value
|
||||
private? && in_dossier_revision? && visible?
|
||||
private? && visible?
|
||||
else
|
||||
false
|
||||
end
|
||||
|
@ -27,9 +27,5 @@ module ChampsValidateConcern
|
|||
def validate_champ_value_or_prefill?
|
||||
validate_champ_value? || validation_context == :prefill
|
||||
end
|
||||
|
||||
def in_dossier_revision?
|
||||
dossier.revision.types_de_champ.any? { _1.stable_id == stable_id }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,7 +33,6 @@ module DossierChampsConcern
|
|||
end
|
||||
|
||||
def project_champ(type_de_champ, row_id)
|
||||
check_valid_row_id?(type_de_champ, row_id)
|
||||
champ = champs_by_public_id[type_de_champ.public_id(row_id)]
|
||||
if champ.nil?
|
||||
type_de_champ.build_champ(dossier: self, row_id:)
|
||||
|
@ -57,8 +56,8 @@ module DossierChampsConcern
|
|||
revision
|
||||
.types_de_champ
|
||||
.filter { _1.stable_id.in?(stable_ids) }
|
||||
.filter { !_1.child?(revision) }
|
||||
.map { _1.repetition? ? project_champ(_1, nil) : champ_for_update(_1, nil, updated_by: nil) }
|
||||
.filter { !revision.child?(_1) }
|
||||
.map { champ_for_update(_1, nil, updated_by: nil) }
|
||||
end
|
||||
|
||||
def champ_for_update(type_de_champ, row_id, updated_by:)
|
||||
|
@ -82,7 +81,6 @@ module DossierChampsConcern
|
|||
end
|
||||
|
||||
def champ_for_export(type_de_champ, row_id)
|
||||
check_valid_row_id?(type_de_champ, row_id)
|
||||
champ = champs_by_public_id[type_de_champ.public_id(row_id)]
|
||||
if champ.blank? || !champ.visible?
|
||||
nil
|
||||
|
@ -98,7 +96,6 @@ module DossierChampsConcern
|
|||
end
|
||||
|
||||
def champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
|
||||
check_valid_row_id?(type_de_champ, row_id)
|
||||
attributes = type_de_champ.params_for_champ
|
||||
# TODO: Once we have the right index in place, we should change this to use `create_or_find_by` instead of `find_or_create_by`
|
||||
champ = champs
|
||||
|
@ -127,14 +124,4 @@ module DossierChampsConcern
|
|||
|
||||
[champ, attributes]
|
||||
end
|
||||
|
||||
def check_valid_row_id?(type_de_champ, row_id)
|
||||
if type_de_champ.child?(revision)
|
||||
if row_id.blank?
|
||||
raise "type_de_champ #{type_de_champ.stable_id} must have a row_id because it is part of a repetition"
|
||||
end
|
||||
elsif row_id.present?
|
||||
raise "type_de_champ #{type_de_champ.stable_id} can not have a row_id because it is not part of a repetition"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,7 +19,7 @@ module DossierSectionsConcern
|
|||
end
|
||||
|
||||
def auto_numbering_section_headers_for?(type_de_champ)
|
||||
return false if type_de_champ.child?(revision)
|
||||
return false if revision.child?(type_de_champ)
|
||||
|
||||
sections_for(type_de_champ)&.none? { _1.libelle =~ /^\d/ }
|
||||
end
|
||||
|
|
|
@ -51,6 +51,7 @@ class Dossier < ApplicationRecord
|
|||
has_many :champs_to_destroy, -> { order(:parent_id) }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
||||
has_many :champs_public, -> { root.public_only }, class_name: 'Champ', inverse_of: false
|
||||
has_many :champs_private, -> { root.private_only }, class_name: 'Champ', inverse_of: false
|
||||
has_many :prefilled_champs_public, -> { root.public_only.prefilled }, class_name: 'Champ', inverse_of: false
|
||||
|
||||
has_many :commentaires, inverse_of: :dossier, dependent: :destroy
|
||||
has_many :preloaded_commentaires, -> { includes(:dossier_correction, piece_jointe_attachments: :blob) }, class_name: 'Commentaire', inverse_of: :dossier
|
||||
|
@ -269,16 +270,33 @@ class Dossier < ApplicationRecord
|
|||
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }
|
||||
scope :without_followers, -> { where.missing(:follows) }
|
||||
scope :with_followers, -> { left_outer_joins(:follows).where.not(follows: { id: nil }) }
|
||||
scope :with_champs, -> {
|
||||
includes(champs_public: [
|
||||
:geo_areas,
|
||||
piece_justificative_file_attachments: :blob,
|
||||
champs: [piece_justificative_file_attachments: :blob]
|
||||
])
|
||||
}
|
||||
|
||||
scope :brouillons_recently_updated, -> { updated_since(2.days.ago).state_brouillon.order_by_updated_at }
|
||||
scope :with_annotations, -> {
|
||||
includes(champs_private: [
|
||||
:geo_areas,
|
||||
piece_justificative_file_attachments: :blob,
|
||||
champs: [piece_justificative_file_attachments: :blob]
|
||||
])
|
||||
}
|
||||
scope :for_api, -> {
|
||||
includes(commentaires: { piece_jointe_attachments: :blob },
|
||||
justificatif_motivation_attachment: :blob,
|
||||
attestation: [],
|
||||
avis: { piece_justificative_file_attachment: :blob },
|
||||
traitement: [],
|
||||
etablissement: [],
|
||||
individual: [],
|
||||
user: [])
|
||||
with_champs
|
||||
.with_annotations
|
||||
.includes(commentaires: { piece_jointe_attachments: :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
|
||||
|
@ -423,6 +441,8 @@ class Dossier < ApplicationRecord
|
|||
validates :mandataire_last_name, presence: true, if: :for_tiers?
|
||||
validates :for_tiers, inclusion: { in: [true, false] }, if: -> { revision&.procedure&.for_individual? }
|
||||
|
||||
validates_associated :prefilled_champs_public, on: :champs_public_value
|
||||
|
||||
def types_de_champ_public
|
||||
types_de_champ
|
||||
end
|
||||
|
@ -929,7 +949,7 @@ class Dossier < ApplicationRecord
|
|||
end
|
||||
|
||||
def remove_titres_identite!
|
||||
champs.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later)
|
||||
champs_public.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later)
|
||||
end
|
||||
|
||||
def remove_piece_justificative_file_not_visible!
|
||||
|
@ -1144,7 +1164,7 @@ class Dossier < ApplicationRecord
|
|||
end
|
||||
|
||||
def has_annotations?
|
||||
revision.types_de_champ_private.present?
|
||||
revision.revision_types_de_champ_private.present?
|
||||
end
|
||||
|
||||
def hide_info_with_accuse_lecture?
|
||||
|
|
|
@ -37,12 +37,18 @@ class DossierPreloader
|
|||
|
||||
private
|
||||
|
||||
def revisions(pj_template: false)
|
||||
def revisions
|
||||
@revisions ||= ProcedureRevision.where(id: @dossiers.pluck(:revision_id).uniq)
|
||||
.includes(types_de_champ: pj_template ? { piece_justificative_template_attachment: :blob } : [])
|
||||
.includes(types_de_champ: { piece_justificative_template_attachment: :blob })
|
||||
.index_by(&:id)
|
||||
end
|
||||
|
||||
# returns: { revision_id : { stable_id : position } }
|
||||
def positions
|
||||
@positions ||= revisions
|
||||
.transform_values { |revision| revision.revision_types_de_champ.map { [_1.stable_id, _1.position] }.to_h }
|
||||
end
|
||||
|
||||
def load_dossiers(dossiers, pj_template: false)
|
||||
to_include = @includes_for_champ.dup
|
||||
to_include << [piece_justificative_file_attachments: :blob]
|
||||
|
@ -52,10 +58,16 @@ class DossierPreloader
|
|||
.where(dossier_id: dossiers)
|
||||
.to_a
|
||||
|
||||
champs_by_dossier = all_champs.group_by(&:dossier_id)
|
||||
children_champs, root_champs = all_champs.partition(&:child?)
|
||||
champs_by_dossier = root_champs.group_by(&:dossier_id)
|
||||
champs_by_dossier_by_parent = children_champs
|
||||
.group_by(&:dossier_id)
|
||||
.transform_values do |champs|
|
||||
champs.group_by(&:parent_id)
|
||||
end
|
||||
|
||||
dossiers.each do |dossier|
|
||||
load_dossier(dossier, champs_by_dossier[dossier.id] || [], pj_template:)
|
||||
load_dossier(dossier, champs_by_dossier[dossier.id] || [], champs_by_dossier_by_parent[dossier.id] || {})
|
||||
end
|
||||
|
||||
load_etablissements(all_champs)
|
||||
|
@ -74,30 +86,17 @@ class DossierPreloader
|
|||
end
|
||||
end
|
||||
|
||||
def load_dossier(dossier, champs, pj_template: false)
|
||||
revision = revisions(pj_template:)[dossier.revision_id]
|
||||
def load_dossier(dossier, champs, children_by_parent = {})
|
||||
revision = revisions[dossier.revision_id]
|
||||
if revision.present?
|
||||
dossier.association(:revision).target = revision
|
||||
end
|
||||
dossier.association(:champs).target = champs
|
||||
dossier.association(:champs_public).target = dossier.champs_for_revision(scope: :public, root: true)
|
||||
dossier.association(:champs_private).target = dossier.champs_for_revision(scope: :private, root: true)
|
||||
|
||||
# remove once parent_id is deprecated
|
||||
champs_by_parent_id = champs.group_by(&:parent_id)
|
||||
champs_public, champs_private = champs.partition(&:public?)
|
||||
|
||||
champs.each do |champ|
|
||||
champ.association(:dossier).target = dossier
|
||||
|
||||
# remove once parent_id is deprecated
|
||||
if champ.repetition?
|
||||
children = champs_by_parent_id.fetch(champ.id, [])
|
||||
children.each do |child|
|
||||
child.association(:parent).target = champ
|
||||
end
|
||||
champ.association(:champs).target = children
|
||||
end
|
||||
end
|
||||
dossier.association(:champs).target = []
|
||||
load_champs(dossier, :champs_public, champs_public, dossier, children_by_parent)
|
||||
load_champs(dossier, :champs_private, champs_private, dossier, children_by_parent)
|
||||
|
||||
# We need to do this because of the check on `Etablissement#champ` in
|
||||
# `Etablissement#libelle_for_export`. By assigning `nil` to `target` we mark association
|
||||
|
@ -106,4 +105,33 @@ class DossierPreloader
|
|||
dossier.etablissement.association(:champ).target = nil
|
||||
end
|
||||
end
|
||||
|
||||
def load_champs(parent, name, champs, dossier, children_by_parent)
|
||||
if champs.empty?
|
||||
parent.association(name).target = [] # tells to Rails association has been loaded
|
||||
return
|
||||
end
|
||||
|
||||
champs.each do |champ|
|
||||
champ.association(:dossier).target = dossier
|
||||
|
||||
if parent.is_a?(Champ)
|
||||
champ.association(:parent).target = parent
|
||||
end
|
||||
end
|
||||
|
||||
dossier.association(:champs).target += champs
|
||||
|
||||
parent.association(name).target = champs
|
||||
.filter { positions[dossier.revision_id][_1.stable_id].present? }
|
||||
.sort_by { [_1.row_id, positions[dossier.revision_id][_1.stable_id]] }
|
||||
|
||||
# Load children champs
|
||||
champs.filter(&:block?).each do |parent_champ|
|
||||
champs = children_by_parent[parent_champ.id] || []
|
||||
parent_champ.association(:dossier).target = dossier
|
||||
|
||||
load_champs(parent_champ, :champs, champs, dossier, children_by_parent)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -207,6 +207,20 @@ class Procedure < ApplicationRecord
|
|||
includes(:draft_revision, :published_revision, administrateurs: :user)
|
||||
}
|
||||
|
||||
scope :for_download, -> {
|
||||
includes(
|
||||
:groupe_instructeurs,
|
||||
dossiers: {
|
||||
champs_public: [
|
||||
piece_justificative_file_attachments: :blob,
|
||||
champs: [
|
||||
piece_justificative_file_attachments: :blob
|
||||
]
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
validates :libelle, presence: true, allow_blank: false, allow_nil: false
|
||||
validates :description, presence: true, allow_blank: false, allow_nil: false
|
||||
validates :administrateurs, presence: true
|
||||
|
|
|
@ -219,6 +219,11 @@ class ProcedureRevision < ApplicationRecord
|
|||
.find { _1.type_de_champ_id == tdc.id }.parent&.type_de_champ
|
||||
end
|
||||
|
||||
def child?(tdc)
|
||||
revision_types_de_champ
|
||||
.find { _1.type_de_champ_id == tdc.id }.child?
|
||||
end
|
||||
|
||||
def remove_children_of(tdc)
|
||||
children_of(tdc).each do |child|
|
||||
remove_type_de_champ(child.stable_id)
|
||||
|
|
|
@ -32,8 +32,8 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
|
|||
end
|
||||
|
||||
def siblings
|
||||
if child?
|
||||
revision.revision_types_de_champ.where(parent_id:).ordered
|
||||
if parent_id.present?
|
||||
revision.revision_types_de_champ.where(parent_id: parent_id).ordered
|
||||
elsif private?
|
||||
revision.revision_types_de_champ_private
|
||||
else
|
||||
|
|
|
@ -456,10 +456,6 @@ class TypeDeChamp < ApplicationRecord
|
|||
!private?
|
||||
end
|
||||
|
||||
def child?(revision)
|
||||
revision.revision_types_de_champ.find { _1.type_de_champ_id == id }&.child?
|
||||
end
|
||||
|
||||
def filename_for_attachement(attachment_sym)
|
||||
attachment = send(attachment_sym)
|
||||
if attachment.attached?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue