-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly recurse over nested field associations
- Loading branch information
Showing
5 changed files
with
42 additions
and
53 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,28 +1,11 @@ | ||
class DialogFieldAssociationValidator | ||
def circular_references(associations) | ||
return false if associations.empty? | ||
association_path_list = initial_paths(associations) | ||
association_path_list.each do |path| | ||
fieldname_being_triggered = path.last | ||
next if associations[fieldname_being_triggered].blank? | ||
circular_references = walk_value_path(fieldname_being_triggered, associations, path) | ||
return circular_references if circular_references.present? | ||
end | ||
false | ||
end | ||
|
||
private | ||
|
||
def initial_paths(associations) | ||
associations.flat_map { |key, values| values.map { |value| [key, value] } } | ||
end | ||
|
||
def walk_value_path(fieldname_being_triggered, associations, path) | ||
while associations[fieldname_being_triggered].present? | ||
return [fieldname_being_triggered, associations[fieldname_being_triggered].first] if path.include?(associations[fieldname_being_triggered].first) | ||
path << associations[fieldname_being_triggered] | ||
path.flatten! | ||
fieldname_being_triggered = path.last | ||
class DialogFieldAssociationCircularReferenceError < RuntimeError; end | ||
def check_for_circular_references(hash, k, collection = []) | ||
raise DialogFieldAssociationCircularReferenceError, "#{k} already exists in #{collection}" if collection.include?(k) | ||
collection << k | ||
hash[k]&.each do |val| | ||
check_for_circular_references(hash, val, collection.dup) | ||
end | ||
nil | ||
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
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
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
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