From b5eb87de6a8403392c7f68ab075e381acf508624 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 23 Sep 2024 17:05:10 +0200 Subject: [PATCH] fix(champ): improve row_id validation and remove it during export --- app/models/concerns/dossier_champs_concern.rb | 7 +++---- app/models/type_de_champ.rb | 6 +++++- spec/models/concerns/dossier_champs_concern_spec.rb | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/dossier_champs_concern.rb b/app/models/concerns/dossier_champs_concern.rb index 8e1f3d57a..501fdda56 100644 --- a/app/models/concerns/dossier_champs_concern.rb +++ b/app/models/concerns/dossier_champs_concern.rb @@ -82,7 +82,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 @@ -131,10 +130,10 @@ module DossierChampsConcern 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" + raise "type_de_champ #{type_de_champ.stable_id} in revision #{revision_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" + elsif row_id.present? && type_de_champ.in_revision?(revision) + raise "type_de_champ #{type_de_champ.stable_id} in revision #{revision_id} can not have a row_id because it is not part of a repetition" end end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index f8640127a..ea8d30dcc 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -456,8 +456,12 @@ class TypeDeChamp < ApplicationRecord !private? end + def in_revision?(revision) + revision.types_de_champ.any? { _1.stable_id == stable_id } + end + def child?(revision) - revision.revision_types_de_champ.find { _1.type_de_champ_id == id }&.child? + revision.revision_types_de_champ.find { _1.stable_id == stable_id }&.child? end def filename_for_attachement(attachment_sym) diff --git a/spec/models/concerns/dossier_champs_concern_spec.rb b/spec/models/concerns/dossier_champs_concern_spec.rb index 5e68d5e99..0d88a4243 100644 --- a/spec/models/concerns/dossier_champs_concern_spec.rb +++ b/spec/models/concerns/dossier_champs_concern_spec.rb @@ -55,7 +55,7 @@ RSpec.describe DossierChampsConcern do context "invalid row_id" do let(:type_de_champ_public) { dossier.find_type_de_champ_by_stable_id(99) } it { - expect { subject }.to raise_error("type_de_champ #{type_de_champ_public.stable_id} can not have a row_id because it is not part of a repetition") + expect { subject }.to raise_error("type_de_champ #{type_de_champ_public.stable_id} in revision #{dossier.revision_id} can not have a row_id because it is not part of a repetition") } end end @@ -81,7 +81,7 @@ RSpec.describe DossierChampsConcern do context "invalid row_id" do let(:type_de_champ_public) { dossier.find_type_de_champ_by_stable_id(99) } it { - expect { subject }.to raise_error("type_de_champ #{type_de_champ_public.stable_id} can not have a row_id because it is not part of a repetition") + expect { subject }.to raise_error("type_de_champ #{type_de_champ_public.stable_id} in revision #{dossier.revision_id} can not have a row_id because it is not part of a repetition") } end end