diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-07-06 00:59:45 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-07-06 00:59:45 +0900 |
commit | 9a9aaa564a0873fc4b5f4fc8ee01636fb824e628 (patch) | |
tree | b9215c47e75c9ddfb73e438def1cde5942560b03 /app/validators | |
parent | 1acaf75d9f3afea468bf4116602d9f27ac71dc2d (diff) | |
download | gitlab-ce-9a9aaa564a0873fc4b5f4fc8ee01636fb824e628.tar.gz |
Implement ayufan validator2
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/variable_duplicates_validator.rb | 13 |
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 |