lib/neo4j/rails/validations/uniqueness.rb in neo4j-1.0.0.beta.15 vs lib/neo4j/rails/validations/uniqueness.rb in neo4j-1.0.0.beta.16
- old
+ new
@@ -3,19 +3,19 @@
class UniquenessValidator < ActiveModel::EachValidator
def initialize(options)
super(options.reverse_merge(:case_sensitive => true))
end
- def validate_each(record, attribute, value)
- clazz = record.class
-
- # TODO is it possible to move this to setup instead so that we don't have to do this always ?
- if clazz.index_type_for(attribute) != :exact
- raise "Can't validate property #{attribute} on class #{clazz} since there is no :exact lucene index on that property"
+ def setup(klass)
+ @attributes.each do |attribute|
+ if klass.index_type_for(attribute) != :exact
+ raise "Can't validate property #{attribute} on class #{klass} since there is no :exact lucene index on that property or the index declaration #{attribute} comes after the validation declaration in #{klass} (try to move it before the validation rules)"
+ end
end
+ end
- query = "#{attribute}: #{value}"
- if !clazz.find(query).empty?
+ def validate_each(record, attribute, value)
+ if record.class.find("#{attribute}: #{value}")
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
end
end