Class: Ambry::ActiveModel::Validations::Uniqueness

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/ambry/active_model.rb

Overview

A uniqueness validator, similar to the one provided by Active Record.

Instance Method Summary (collapse)

Instance Method Details

- (Object) validate_each(record, attribute, value)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ambry/active_model.rb', line 27

def validate_each(record, attribute, value)
  return if record.persisted?
  if attribute.to_sym == record.class.id_method
    begin
      if record.class.mapper[value]
        record.errors[attribute] << "must be unique"
      end
    rescue Ambry::NotFoundError
    end
  else
    if record.class.all.detect {|x| x.send(attribute) == value}
      record.errors[attribute] << "must be unique"
    end
  end
end