Sha256: 91d66be603018c55dc09c968e47d5fa4d695d9b5c39f95575c8004b41350ab96
Contents?: true
Size: 1.59 KB
Versions: 4
Compression:
Stored size: 1.59 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 # Unfortunately, we have to tie Uniqueness validators to a class. def setup(klass) @klass = klass end def validate_each(document, attribute, value) if document.embedded? return if document._parent.nil? criteria = document._parent.send(document.association_name) # If the parent document embeds_one, no need to validate uniqueness return if criteria.is_a?(Mongoid::Document) criteria = criteria.where(attribute => value, :_id => {'$ne' => document._id}) else criteria = @klass.where(attribute => value) unless document.new_record? criteria = criteria.where(:_id => {'$ne' => document._id}) end end Array.wrap(options[:scope]).each do |item| criteria = criteria.where(item => document.attributes[item]) end if criteria.exists? 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
4 entries across 4 versions & 2 rubygems