Merge pull request #8851 from demarches-simplifiees/fix_condtion_check_on_children

fix(condition_check_on_children): give proper upper tdc to child cond…
This commit is contained in:
mfo 2023-04-04 08:48:02 +00:00 committed by GitHub
commit 2c32e38383
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View file

@ -390,12 +390,14 @@ class ProcedureRevision < ApplicationRecord
def conditions_are_valid?
public_tdcs = types_de_champ_public.to_a
.flat_map { _1.repetition? ? children_of(_1) : _1 }
public_tdcs
.flat_map { _1.repetition? ? children_of(_1) : _1 }
.map.with_index
.filter_map { |tdc, i| tdc.condition? ? [tdc, i] : nil }
.map { |tdc, i| [tdc, tdc.condition.errors(public_tdcs.take(i))] }
.map do |tdc, i|
[tdc, tdc.condition.errors(public_tdcs.take(i))]
end
.filter { |_tdc, errors| errors.present? }
.each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) }
end

View file

@ -860,22 +860,33 @@ describe ProcedureRevision do
it { expect(subject.first.attribute).to eq(:condition) }
end
context 'when a champ belongs to a repetition' do
context 'with a repetition' do
let(:procedure) do
create(:procedure,
types_de_champ_public: [{ type: :repetition, children: [{ type: :integer_number }, { type: :text }] }])
end
let(:condition) { ds_eq(constant(true), constant(1)) }
before do
let(:children_of_repetition) do
repetition = procedure.draft_revision.types_de_champ_public.find(&:repetition?)
champs_repetition = procedure.draft_revision.children_of(repetition)
integer_champ = champs_repetition.first
text_champ = champs_repetition.last
text_champ.update(condition: condition)
procedure.draft_revision.children_of(repetition)
end
it { expect(subject.first.attribute).to eq(:condition) }
let(:integer_champ) { children_of_repetition.first }
let(:text_champ) { children_of_repetition.last }
before { text_champ.update(condition: condition) }
context 'when a child champ has a valid condition' do
let(:condition) { ds_eq(champ_value(integer_champ.stable_id), constant(1)) }
it { is_expected.to be_empty }
end
context 'when a champ belongs to a repetition' do
let(:condition) { ds_eq(champ_value(-1), constant(1)) }
it { expect(subject.first.attribute).to eq(:condition) }
end
end
end