fix(champ): improve row_id validation and remove it during export

This commit is contained in:
Paul Chavard 2024-09-23 17:05:10 +02:00
parent 8bf6272c9b
commit b5eb87de6a
No known key found for this signature in database
3 changed files with 10 additions and 7 deletions

View file

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

View file

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

View file

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