refactor(dossier): implement champs_for_revision

This commit is contained in:
Paul Chavard 2023-11-28 16:22:17 +00:00
parent 9280ccc23d
commit 283ee72d51
2 changed files with 30 additions and 0 deletions

View file

@ -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

View file

@ -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