Class: Armada::Validations::UniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/armada/validations.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/armada/validations.rb', line 9

def validate_each(record, attribute, value)
  relation = record.class.where(attribute => value)
  
  Array.wrap(options[:scope]).each do |scope_attribute|
    relation = relation.where(scope_attribute => record.attributes[scope_attribute])
  end
  
  relation = relation.where(:id => {"!=" => record.id}) if record.persisted?
  
  return if relation.count == 0
  record.errors.add(attribute, :taken, :default => options[:message], :value => value)
end