module Mongoid::Validations::ClassMethods

Public Instance Methods

validates_relation(metadata) click to toggle source

Adds an associated validator for the relation if the validate option was not provided or set to true.

@example Set up validation.

Person.validates_relation(metadata)

@param [ Metadata ] metadata The relation metadata.

@since 2.0.0.rc.1

# File lib/mongoid/validations.rb, line 121
def validates_relation(metadata)
  if metadata.validate?
    validates_associated(metadata.name)
  end
end
validates_with(*args, &block) click to toggle source

Add validation with the supplied validators forthe provided fields with options.

@example Validate with a specific validator.

validates_with MyValidator, on: :create

@param [ Class<Array>, Hash ] *args The validator classes and options.

@note See ActiveModel::Validations::With for full options. This is

overridden to add autosave functionality when presence validation is
added.

@since 3.0.0

Calls superclass method
# File lib/mongoid/validations.rb, line 140
def validates_with(*args, &block)
  if args.first == PresenceValidator
    args.last[:attributes].each do |name|
      metadata = relations[name.to_s]
      if metadata && metadata[:autosave] != false
        autosave(metadata.merge!(autosave: true))
      end
    end
  end
  super
end
validating_with_query?() click to toggle source

Are we currently performing a validation that has a query?

@example Are we validating with a query?

Model.validating_with_query?

@return [ true, false ] If we are validating with a query.

@since 3.0.2

# File lib/mongoid/validations.rb, line 160
def validating_with_query?
  Threaded.executing?("#{name}-validate-with-query")
end