Refactor some validaors to different file

This commit is contained in:
Martin Lensment 2015-01-02 11:31:59 +02:00
parent 7ebec5ad5d
commit cd7151b463
3 changed files with 26 additions and 42 deletions

View file

@ -0,0 +1,14 @@
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])
record.errors.add(attribute, :invalid) if record.errors[attribute].blank?
x.errors.add(options[:attribute], :taken)
end
end
end