mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 01:33:36 +02:00
Refactor some validaors to different file
This commit is contained in:
parent
7ebec5ad5d
commit
cd7151b463
3 changed files with 26 additions and 42 deletions
14
app/validators/uniqueness_multi_validator.rb
Normal file
14
app/validators/uniqueness_multi_validator.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue