Module: Mongoid::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Components
Defined in:
lib/mongoid/validations.rb,
lib/mongoid/validations/associated.rb,
lib/mongoid/validations/referenced.rb,
lib/mongoid/validations/uniqueness.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods Classes: AssociatedValidator, ReferencedValidator, UniquenessValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#validatedObject

Returns the value of attribute validated.



14
15
16
# File 'lib/mongoid/validations.rb', line 14

def validated
  @validated
end

Instance Method Details

#read_attribute_for_validation(attr) ⇒ Object

Overrides the default ActiveModel behaviour since we need to handle validations of relations 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 relation.

Returns:

  • (Object)

    The value of the field or the relation.

Since:

  • 2.0.0.rc.1



28
29
30
31
32
33
34
# File 'lib/mongoid/validations.rb', line 28

def read_attribute_for_validation(attr)
  if relations[attr.to_s]
    send(attr, false, :eager => true)
  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.

Since:

  • 2.0.0.rc.6



49
50
51
# File 'lib/mongoid/validations.rb', line 49

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

#validated?true, false

Used to prevent infinite loops in associated validations.

Examples:

Is the document validated?

document.validated?

Returns:

  • (true, false)

    Has the document already been validated?

Since:

  • 2.0.0.rc.2



61
62
63
# File 'lib/mongoid/validations.rb', line 61

def validated?
  !!@validated
end