Class: Mobility::ActiveRecord::UniquenessValidator

Inherits:
ActiveRecord::Validations::UniquenessValidator
  • Object
show all
Defined in:
lib/mobility/active_record/uniqueness_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mobility/active_record/uniqueness_validator.rb', line 4

def validate_each(record, attribute, value)
  klass = record.class

  if ((Array(options[:scope]) + [attribute]).map(&:to_s) & klass.translated_attribute_names).present?
    return unless value.present?
    relation = klass.send(Mobility.query_method).where(attribute => value)
    relation = relation.where.not(klass.primary_key => record.id) if record.persisted?
    relation = mobility_scope_relation(record, relation)
    relation = relation.merge(options[:conditions]) if options[:conditions]

    if relation.exists?
      error_options = options.except(:case_sensitive, :scope, :conditions)
      error_options[:value] = value

      record.errors.add(attribute, :taken, error_options)
    end
  else
    super
  end
end