-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10508 from mfo/US/fix-annotation-having-condition…
…-on-public-tdc-with-smaller-position-than-the-annotation ETQ admin, je veux que mes conditions d'annotations privées ne soient pas en erreur pour des raisons d'ordre
- Loading branch information
Showing
2 changed files
with
49 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
class TypesDeChamp::ConditionValidator < ActiveModel::EachValidator | ||
def validate_each(procedure, attribute, types_de_champ) | ||
return if types_de_champ.empty? | ||
# condition are valid when | ||
# 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 | ||
procedure.draft_revision.types_de_champ_for | ||
else | ||
procedure.draft_revision.types_de_champ_for(scope: :public) | ||
end | ||
|
||
tdcs.each_with_index do |tdc, i| | ||
tdcs = tdcs_with_children(procedure, tdcs) | ||
tdcs.each_with_index do |tdc, tdc_index| | ||
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? | ||
|
||
procedure.errors.add( | ||
attribute, | ||
procedure.errors.generate_message(attribute, :invalid_condition, { value: tdc.libelle }), | ||
collection, | ||
procedure.errors.generate_message(collection, :invalid_condition, { value: tdc.libelle }), | ||
type_de_champ: tdc | ||
) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters