Sha256: b7888740106e7b1143026a458e197147fba3120b2ad05a595f520824f63384f4

Contents?: true

Size: 965 Bytes

Versions: 9

Compression:

Stored size: 965 Bytes

Contents

module Neo4j
  module Validations
    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"
        end

        query = "#{attribute}: #{value}"
        if !clazz.find(query).empty?
          record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
        end
      end
    end

    module ClassMethods
      def validates_uniqueness_of(*attr_names)
        validates_with UniquenessValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
neo4j-1.0.0.beta.15 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.14 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.13 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.12 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.11 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.10 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.9 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.8 lib/neo4j/rails/validations/uniqueness.rb
neo4j-1.0.0.beta.7 lib/neo4j/rails/validations/uniqueness.rb