123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Validatable

Overview

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

Instance Attribute Summary

Instance Method Summary

DSL Calls

included

[ GitHub ]


20
21
22
23
# File 'lib/mongoid/validatable.rb', line 20

included do
  extend Macros
  include Macros
end

Instance Attribute Details

#validated?true | false (readonly)

Used to prevent infinite loops in associated validations.

Examples:

Is the document validated?

document.validated?

Returns:

  • (true | false)

    Has the document already been validated?

[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 108

def validated?
  Threaded.validated?(self)
end

#validating_with_query?true | false (readonly)

Are we currently performing a validation that has a query?

Examples:

Are we validating with a query?

document.validating_with_query?

Returns:

  • (true | false)

    If we are validating with a query.

[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 118

def validating_with_query?
  self.class.validating_with_query?
end

Instance Method Details

#begin_validate

Begin the associated validation.

Examples:

Begin validation.

document.begin_validate
[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 29

def begin_validate
  Threaded.begin_validate(self)
end

#exit_validate

Exit the associated validation.

Examples:

Exit validation.

document.exit_validate
[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 37

def exit_validate
  Threaded.exit_validate(self)
end

#performing_validations?(options = {}) ⇒ true | false

Given the provided options, are we performing validations?

Examples:

Are we performing validations?

document.performing_validations?(validate: true)

Parameters:

  • options (Hash) (defaults to: {})

    The options to check.

Options Hash (options):

  • :validate (true | false)

    Whether or not to validate.

Returns:

  • (true | false)

    If we are validating.

[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 59

def performing_validations?(options = {})
  options[:validate].nil? ? true : options[:validate]
end

#read_attribute_for_validation(attr) ⇒ Object

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

Examples:

Read the value.

person.read_attribute_for_validation(:addresses)

Parameters:

  • attr (Symbol)

    The name of the field or association.

Returns:

  • (Object)

    The value of the field or the association.

[ GitHub ]

  
# 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) ⇒ true | false

Determine if the document is valid.

Examples:

Is the document valid?

person.valid?

Is the document valid in a context?

person.valid?(:create)

Parameters:

  • context (Symbol) (defaults to: nil)

    The optional validation context.

Returns:

  • (true | false)

    True if valid, false if not.

[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 98

def valid?(context = nil)
  super context ? context : (new_record? ? :create : :update)
end

#validating

Perform a validation within the associated block.

[ GitHub ]

  
# File 'lib/mongoid/validatable.rb', line 42

def validating
  begin_validate
  yield
ensure
  exit_validate
end