From 283ee72d516c0f8f8ec4d78e41aff1c5d3805096 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 28 Nov 2023 16:22:17 +0000 Subject: [PATCH] refactor(dossier): implement champs_for_revision --- app/models/dossier.rb | 14 ++++++++++++++ app/models/procedure_revision.rb | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 8b9f18cdf..e707b06ea 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1391,6 +1391,20 @@ class Dossier < ApplicationRecord user.france_connect_information.present? end + def champs_by_stable_id_with_row + champs_for_revision.index_by(&:stable_id_with_row) + end + + def champs_for_revision(scope = nil, root = false) + champs_index = champs.group_by(&:stable_id) + + if scope.is_a?(TypeDeChamp) + revision.children_of(scope) + else + revision.types_de_champ_for(scope, root) + end.flat_map { champs_index[_1.stable_id] || [] } + end + private def create_missing_traitemets diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 78714241a..8e1d07811 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -155,6 +155,22 @@ class ProcedureRevision < ApplicationRecord dossier end + def types_de_champ_for(scope = nil, root = false) + # We return an unordered collection + return types_de_champ if !root && scope.nil? + return types_de_champ.filter { scope == :public ? _1.public? : _1.private? } if !root + + # We return an ordered collection + case scope + when :public + types_de_champ_public + when :private + types_de_champ_private + else + types_de_champ_public + types_de_champ_private + end + end + def children_of(tdc) if revision_types_de_champ.loaded? parent_coordinate_id = revision_types_de_champ