mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 17:37:17 +02:00
15 lines
632 B
Ruby
15 lines
632 B
Ruby
class UniquenessMultiValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
validated = []
|
|
list = value.reject(&:marked_for_destruction?)
|
|
list.each do |x|
|
|
next if x.send(options[:attribute]).blank?
|
|
existing = list.select { |y| x.send(options[:attribute]) == y.send(options[:attribute]) }
|
|
next unless existing.length > 1
|
|
validated << x.send(options[:attribute])
|
|
association = options[:association] || attribute
|
|
record.errors.add(association, :invalid) if record.errors[association].blank?
|
|
x.errors.add(options[:attribute], :taken)
|
|
end
|
|
end
|
|
end
|