Sha256: 15858b0e203c1d6518404be3deb2d83b063a6a9b1df78d4b47fd1e41f0f79ca9
Contents?: true
Size: 1 KB
Versions: 1
Compression:
Stored size: 1 KB
Contents
# encoding: utf-8 module Mongoid #:nodoc: module Validations #:nodoc: # Validates whether or not a field is unique against the documents in the # database. # # Example: # # class Person # include Mongoid::Document # field :title # # validates_uniqueness_of :title # end class UniquenessValidator < ActiveModel::EachValidator def validate_each(document, attribute, value) conditions = {attribute => value} conditions[options[:scope]] = document.attributes[options[:scope]] if options.has_key? :scope return if document.class.where(conditions).empty? if document.new_record? || key_changed?(document) document.errors.add(attribute, :taken, :default => options[:message], :value => value) end end protected def key_changed?(document) (document.primary_key || {}).each do |key| return true if document.send("#{key}_changed?") end; false end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongoid-2.0.0.beta.5 | lib/mongoid/validations/uniqueness.rb |