diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-07-06 00:45:26 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-07-06 00:45:26 +0900 |
commit | 1acaf75d9f3afea468bf4116602d9f27ac71dc2d (patch) | |
tree | 614ff98bddb81a473f2774386f5ffcf60c2bac60 /app/validators | |
parent | 0c9795a7b63dda457656784d23a88ec7cf800773 (diff) | |
download | gitlab-ce-1acaf75d9f3afea468bf4116602d9f27ac71dc2d.tar.gz |
Implement ayufan validator
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/uniqueness_of_in_memory_validator.rb | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/app/validators/uniqueness_of_in_memory_validator.rb b/app/validators/uniqueness_of_in_memory_validator.rb deleted file mode 100644 index 84e88b2eb76..00000000000 --- a/app/validators/uniqueness_of_in_memory_validator.rb +++ /dev/null @@ -1,37 +0,0 @@ -# UniquenessOfInMemoryValidator -# -# 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 -# -# Inspired by https://stackoverflow.com/a/2883129/2522666 -module ActiveRecord - class Base - # Validate that the the objects in +collection+ are unique - # when compared against all their non-blank +attrs+. If not - # add +message+ to the base errors. - def validate_uniqueness_of_in_memory(collection, attrs, message) - hashes = collection.inject({}) do |hash, record| - key = attrs.map { |a| record.send(a).to_s }.join - if key.blank? || record.marked_for_destruction? - key = record.object_id - end - hash[key] = record unless hash[key] - hash - end - - if collection.length > hashes.length - self.errors.add(*message) - end - end - end -end - -class UniquenessOfInMemoryValidator < ActiveModel::Validator - def validate(record) - record.validate_uniqueness_of_in_memory( - record.public_send(options[:collection]), - options[:attrs], - options[:message]) - end -end |