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:
commit
2c32e38383
2 changed files with 23 additions and 10 deletions
|
@ -390,12 +390,14 @@ class ProcedureRevision < ApplicationRecord
|
||||||
|
|
||||||
def conditions_are_valid?
|
def conditions_are_valid?
|
||||||
public_tdcs = types_de_champ_public.to_a
|
public_tdcs = types_de_champ_public.to_a
|
||||||
|
.flat_map { _1.repetition? ? children_of(_1) : _1 }
|
||||||
|
|
||||||
public_tdcs
|
public_tdcs
|
||||||
.flat_map { _1.repetition? ? children_of(_1) : _1 }
|
|
||||||
.map.with_index
|
.map.with_index
|
||||||
.filter_map { |tdc, i| tdc.condition? ? [tdc, i] : nil }
|
.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? }
|
.filter { |_tdc, errors| errors.present? }
|
||||||
.each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) }
|
.each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -860,22 +860,33 @@ describe ProcedureRevision do
|
||||||
it { expect(subject.first.attribute).to eq(:condition) }
|
it { expect(subject.first.attribute).to eq(:condition) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a champ belongs to a repetition' do
|
context 'with a repetition' do
|
||||||
let(:procedure) do
|
let(:procedure) do
|
||||||
create(:procedure,
|
create(:procedure,
|
||||||
types_de_champ_public: [{ type: :repetition, children: [{ type: :integer_number }, { type: :text }] }])
|
types_de_champ_public: [{ type: :repetition, children: [{ type: :integer_number }, { type: :text }] }])
|
||||||
end
|
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?)
|
repetition = procedure.draft_revision.types_de_champ_public.find(&:repetition?)
|
||||||
champs_repetition = procedure.draft_revision.children_of(repetition)
|
procedure.draft_revision.children_of(repetition)
|
||||||
integer_champ = champs_repetition.first
|
|
||||||
text_champ = champs_repetition.last
|
|
||||||
text_champ.update(condition: condition)
|
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue