Class: Norman::ActiveModel::Validations::Uniqueness

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/norman/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 Norman::NotFoundError
    end
  else
    if record.class.all.detect {|x| x.send(attribute) == value}
      record.errors[attribute] << "must be unique"
    end
  end
end