summaryrefslogtreecommitdiff
path: root/app/validators
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2017-07-06 00:59:45 +0900
committerShinya Maeda <shinya@gitlab.com>2017-07-06 00:59:45 +0900
commit9a9aaa564a0873fc4b5f4fc8ee01636fb824e628 (patch)
treeb9215c47e75c9ddfb73e438def1cde5942560b03 /app/validators
parent1acaf75d9f3afea468bf4116602d9f27ac71dc2d (diff)
downloadgitlab-ce-9a9aaa564a0873fc4b5f4fc8ee01636fb824e628.tar.gz
Implement ayufan validator2
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/variable_duplicates_validator.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/app/validators/variable_duplicates_validator.rb b/app/validators/variable_duplicates_validator.rb
new file mode 100644
index 00000000000..8a9d8892e9b
--- /dev/null
+++ b/app/validators/variable_duplicates_validator.rb
@@ -0,0 +1,13 @@
+# VariableDuplicatesValidator
+#
+# This validtor is designed for especially the following condition
+# - Use `accepts_nested_attributes_for :xxx` in a parent model
+# - Use `validates :xxx, uniqueness: { scope: :xxx_id }` in a child model
+class VariableDuplicatesValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ duplicates = value.reject(&:marked_for_destruction?).group_by(&:key).select { |_, v| v.many? }.map(&:first)
+ if duplicates.any?
+ record.errors.add(attribute, "Duplicate variables: #{duplicates.join(", ")}")
+ end
+ end
+end