Class: ActiveModel::Validations::UniquenessValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_repository/uniqueness.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UniquenessValidator

Returns a new instance of UniquenessValidator.



4
5
6
# File 'lib/active_repository/uniqueness.rb', line 4

def initialize(options)
  super(options.reverse_merge(:case_sensitive => true))
end

Instance Method Details

#setup(klass) ⇒ Object

Unfortunately, we have to tie Uniqueness validators to a class.



9
10
11
# File 'lib/active_repository/uniqueness.rb', line 9

def setup(klass)
  @klass = klass
end

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/active_repository/uniqueness.rb', line 13

def validate_each(record, attribute, value)
  finder_class = record.class.get_model_class

  finder_class.all.each do |object|
    if object.id != record.id && object.send(attribute) == record.send(attribute)
      record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value))
      break
    end
  end
end