From c479d46b4739167da5256cbb5869b34c66c668f9 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 18 Apr 2024 10:09:42 +0200 Subject: [PATCH] refactor(champs): extract dossier champ related methods in to a concern --- app/models/concerns/dossier_champs_concern.rb | 56 +++++++++++++++++++ app/models/dossier.rb | 52 +---------------- 2 files changed, 57 insertions(+), 51 deletions(-) create mode 100644 app/models/concerns/dossier_champs_concern.rb diff --git a/app/models/concerns/dossier_champs_concern.rb b/app/models/concerns/dossier_champs_concern.rb new file mode 100644 index 000000000..9bd0cbd5c --- /dev/null +++ b/app/models/concerns/dossier_champs_concern.rb @@ -0,0 +1,56 @@ +module DossierChampsConcern + extend ActiveSupport::Concern + + def champs_for_revision(scope: nil, root: false) + champs_index = champs.group_by(&:stable_id) + # Due to some bad data we can have multiple copies of the same champ. Ignore extra copy. + .transform_values { _1.sort_by(&:id).uniq(&:row_id) } + + if scope.is_a?(TypeDeChamp) + revision + .children_of(scope) + .flat_map { champs_index[_1.stable_id] || [] } + .filter(&:child?) # TODO: remove once bad data (child champ without a row id) is cleaned + else + revision + .types_de_champ_for(scope:, root:) + .flat_map { champs_index[_1.stable_id] || [] } + end + end + + # Get all the champs values for the types de champ in the final list. + # Dossier might not have corresponding champ – display nil. + # To do so, we build a virtual champ when there is no value so we can call for_export with all indexes + def champs_for_export(types_de_champ, row_id = nil) + types_de_champ.flat_map do |type_de_champ| + champ = champ_for_export(type_de_champ, row_id) + type_de_champ.libelles_for_export.map do |(libelle, path)| + [libelle, champ&.for_export(path)] + end + end + end + + def project_champ(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:) + else + champ + end + end + + private + + def champs_by_public_id + @champs_by_public_id ||= champs.sort_by(&:id).index_by(&:public_id) + end + + def champ_for_export(type_de_champ, row_id) + champ = champs_by_public_id[type_de_champ.public_id(row_id)] + if champ.blank? || !champ.visible? + nil + else + champ + end + end +end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d2aafd943..334d883a7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -9,6 +9,7 @@ class Dossier < ApplicationRecord include DossierSearchableConcern include DossierSectionsConcern include DossierStateConcern + include DossierChampsConcern enum state: { brouillon: 'brouillon', @@ -1036,18 +1037,6 @@ class Dossier < ApplicationRecord columns + champs_for_export(types_de_champ) end - # Get all the champs values for the types de champ in the final list. - # Dossier might not have corresponding champ – display nil. - # To do so, we build a virtual champ when there is no value so we can call for_export with all indexes - def champs_for_export(types_de_champ, row_id = nil) - types_de_champ.flat_map do |type_de_champ| - champ = champ_for_export(type_de_champ, row_id) - type_de_champ.libelles_for_export.map do |(libelle, path)| - [libelle, champ&.for_export(path)] - end - end - end - def linked_dossiers_for(instructeur_or_expert) dossier_ids = champs_for_revision.filter(&:dossier_link?).filter_map(&:value) instructeur_or_expert.dossiers.where(id: dossier_ids) @@ -1141,45 +1130,10 @@ class Dossier < ApplicationRecord user.france_connected_with_one_identity? end - def champs_for_revision(scope: nil, root: false) - champs_index = champs.group_by(&:stable_id) - # Due to some bad data we can have multiple copies of the same champ. Ignore extra copy. - .transform_values { _1.sort_by(&:id).uniq(&:row_id) } - - if scope.is_a?(TypeDeChamp) - revision - .children_of(scope) - .flat_map { champs_index[_1.stable_id] || [] } - .filter(&:child?) # TODO: remove once bad data (child champ without a row id) is cleaned - else - revision - .types_de_champ_for(scope:, root:) - .flat_map { champs_index[_1.stable_id] || [] } - end - end - def has_annotations? revision.revision_types_de_champ_private.present? end - def project_champ(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:) - else - champ - end - end - - def champ_for_export(type_de_champ, row_id) - champ = champs_by_public_id[type_de_champ.public_id(row_id)] - if champ.blank? || !champ.visible? - nil - else - champ - end - end - def hide_info_with_accuse_lecture? procedure.accuse_lecture? && termine? && accuse_lecture_agreement_at.blank? end @@ -1190,10 +1144,6 @@ class Dossier < ApplicationRecord private - def champs_by_public_id - @champs_by_public_id ||= champs.sort_by(&:id).index_by(&:public_id) - end - def create_missing_traitemets if en_construction_at.present? && traitements.en_construction.empty? self.traitements.passer_en_construction(processed_at: en_construction_at)