module Mongoid::Validatable

This module provides additional validations that ActiveModel does not provide: validates_associated and validates_uniqueness_of.

Public Instance Methods

begin_validate() click to toggle source

Begin the associated validation.

@example Begin validation.

document.begin_validate
# File lib/mongoid/validatable.rb, line 29
def begin_validate
  Threaded.begin_validate(self)
end
exit_validate() click to toggle source

Exit the associated validation.

@example Exit validation.

document.exit_validate
# File lib/mongoid/validatable.rb, line 37
def exit_validate
  Threaded.exit_validate(self)
end
performing_validations?(options = {}) click to toggle source

Given the provided options, are we performing validations?

@example Are we performing validations?

document.performing_validations?(validate: true)

@param [ Hash ] options The options to check.

@option options [ true | false ] :validate Whether or not to validate.

@return [ true | false ] If we are validating.

# File lib/mongoid/validatable.rb, line 59
def performing_validations?(options = {})
  options[:validate].nil? ? true : options[:validate]
end
read_attribute_for_validation(attr) click to toggle source

Overrides the default ActiveModel behavior since we need to handle validations of associations slightly different than just calling the getter.

@example Read the value.

person.read_attribute_for_validation(:addresses)

@param [ Symbol ] attr The name of the field or association.

@return [ Object ] The value of the field or the association.

# File lib/mongoid/validatable.rb, line 73
def read_attribute_for_validation(attr)
  attribute = database_field_name(attr)
  if relations.key?(attribute)
    begin_validate
    relation = without_autobuild { send(attr) }
    exit_validate
    relation.try(:in_memory) || relation
  elsif fields[attribute].try(:localized?)
    attributes[attribute]
  else
    send(attr)
  end
end
valid?(context = nil) click to toggle source

Determine if the document is valid.

@example Is the document valid?

person.valid?

@example Is the document valid in a context?

person.valid?(:create)

@param [ Symbol ] context The optional validation context.

@return [ true | false ] True if valid, false if not.

Calls superclass method
# File lib/mongoid/validatable.rb, line 98
def valid?(context = nil)
  super context ? context : (new_record? ? :create : :update)
end
validated?() click to toggle source

Used to prevent infinite loops in associated validations.

@example Is the document validated?

document.validated?

@return [ true | false ] Has the document already been validated?

# File lib/mongoid/validatable.rb, line 108
def validated?
  Threaded.validated?(self)
end
validating() { || ... } click to toggle source

Perform a validation within the associated block.

# File lib/mongoid/validatable.rb, line 42
def validating
  begin_validate
  yield
ensure
  exit_validate
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?

document.validating_with_query?

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

# File lib/mongoid/validatable.rb, line 118
def validating_with_query?
  self.class.validating_with_query?
end