fix(draft_types_de_champ_private.condition): condition must be validated with upper_tdcs. considering that types_de_champ_private can have a condition using a types_de_champ_public, we have to include all types_de_champs_public plus only types_de_champs_private.upper_tdcs

This commit is contained in:
mfo 2024-06-11 11:48:21 +02:00
parent 8cb902821f
commit 06a870a083

View file

@ -1,24 +1,34 @@
class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator
def validate_each(procedure, attribute, types_de_champ) # condition are valid when
return if types_de_champ.empty? # tdc.condition.left is present in upper tdcs
# in case of types_de_champ_private, we should include types_de_champ_publics too
def validate_each(procedure, collection, tdcs)
return if tdcs.empty?
tdcs = if attribute == :draft_types_de_champ_private tdcs = tdcs_with_children(procedure, tdcs)
procedure.draft_revision.types_de_champ_for tdcs.each_with_index do |tdc, tdc_index|
else
procedure.draft_revision.types_de_champ_for(scope: :public)
end
tdcs.each_with_index do |tdc, i|
next unless tdc.condition? next unless tdc.condition?
errors = tdc.condition.errors(tdcs.take(i)) upper_tdcs = []
if collection == :draft_types_de_champ_private # in case of private tdc validation, we must include public tdcs
upper_tdcs += tdcs_with_children(procedure, procedure.draft_types_de_champ_public)
end
upper_tdcs += tdcs.take(tdc_index) # we take all upper_tdcs of current tdcs
errors = tdc.condition.errors(upper_tdcs)
next if errors.blank? next if errors.blank?
procedure.errors.add( procedure.errors.add(
attribute, collection,
procedure.errors.generate_message(attribute, :invalid_condition, { value: tdc.libelle }), procedure.errors.generate_message(collection, :invalid_condition, { value: tdc.libelle }),
type_de_champ: tdc type_de_champ: tdc
) )
end end
end end
# find children in repetitions
def tdcs_with_children(procedure, tdcs)
tdcs.to_a
.flat_map { _1.repetition? ? procedure.draft_revision.children_of(_1) : _1 }
end
end end