validate conditions on revision
This commit is contained in:
parent
758933f6f9
commit
0db27b8718
2 changed files with 57 additions and 0 deletions
|
@ -30,6 +30,8 @@ class ProcedureRevision < ApplicationRecord
|
||||||
|
|
||||||
scope :ordered, -> { order(:created_at) }
|
scope :ordered, -> { order(:created_at) }
|
||||||
|
|
||||||
|
validate :conditions_are_valid?
|
||||||
|
|
||||||
def build_champs
|
def build_champs
|
||||||
types_de_champ_public.map { |tdc| tdc.build_champ(revision: self) }
|
types_de_champ_public.map { |tdc| tdc.build_champ(revision: self) }
|
||||||
end
|
end
|
||||||
|
@ -465,4 +467,14 @@ class ProcedureRevision < ApplicationRecord
|
||||||
last.present? ? last.position + 1 : 0
|
last.present? ? last.position + 1 : 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def conditions_are_valid?
|
||||||
|
stable_ids = types_de_champ_public.map(&:stable_id)
|
||||||
|
|
||||||
|
types_de_champ_public
|
||||||
|
.map.with_index
|
||||||
|
.filter_map { |tdc, i| tdc.condition.present? ? [tdc, i] : nil }
|
||||||
|
.flat_map { |tdc, i| tdc.condition.errors(stable_ids.take(i)) }
|
||||||
|
.each { |message| errors.add(:condition, message) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -623,4 +623,49 @@ describe ProcedureRevision do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'conditions_are_valid' do
|
||||||
|
include Logic
|
||||||
|
|
||||||
|
def first_champ = procedure.draft_revision.types_de_champ_public.first
|
||||||
|
|
||||||
|
def second_champ = procedure.draft_revision.types_de_champ_public.second
|
||||||
|
|
||||||
|
let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 2) }
|
||||||
|
let(:draft_revision) { procedure.draft_revision }
|
||||||
|
let(:condition) { nil }
|
||||||
|
|
||||||
|
subject do
|
||||||
|
draft_revision.save
|
||||||
|
draft_revision.errors
|
||||||
|
end
|
||||||
|
|
||||||
|
before { second_champ.update(condition: condition) }
|
||||||
|
|
||||||
|
context 'when a champ has a valid condition (type)' do
|
||||||
|
let(:condition) { ds_eq(constant(true), constant(true)) }
|
||||||
|
|
||||||
|
it { is_expected.to be_empty }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a champ has a valid condition: needed tdc is up in the forms' do
|
||||||
|
let(:condition) { ds_eq(constant('oui'), champ_value(first_champ.stable_id)) }
|
||||||
|
|
||||||
|
it { is_expected.to be_empty }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a champ has an invalid condition' do
|
||||||
|
let(:condition) { ds_eq(constant(true), constant(1)) }
|
||||||
|
|
||||||
|
it { expect(subject.first.attribute).to eq(:condition) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a champ has an invalid condition: needed tdc is down in the forms' do
|
||||||
|
let(:need_second_champ) { ds_eq(constant('oui'), champ_value(second_champ.stable_id)) }
|
||||||
|
|
||||||
|
before { first_champ.update(condition: need_second_champ) }
|
||||||
|
|
||||||
|
it { expect(subject.first.attribute).to eq(:condition) }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue