Module: Mongoid::State

Included in:
Components
Defined in:
lib/mongoid/state.rb

Overview

This module contains the behaviour for getting the various states a document can transition through.

Instance Method Summary collapse

Instance Method Details

#destroyed=(destroyed) ⇒ true, false

Sets the destroyed boolean - used after document is destroyed.

Examples:

Set the destroyed flag.

person.destroyed = true

Returns:

  • (true, false)

    The value set for destroyed.



63
64
65
# File 'lib/mongoid/state.rb', line 63

def destroyed=(destroyed)
  @destroyed = destroyed && true
end

#destroyed?true, false Also known as: deleted?

Returns true if the Document has been succesfully destroyed, and false if it hasn’t. This is determined by the variable @destroyed and NOT by checking the database.

Examples:

Is the document destroyed?

person.destroyed?

Returns:

  • (true, false)

    True if destroyed, false if not.



52
53
54
# File 'lib/mongoid/state.rb', line 52

def destroyed?
  @destroyed == true
end

#new_record=(saved) ⇒ true, false

Sets the new_record boolean - used after document is saved.

Examples:

Set whether the document is new.

person.new_record = true

Parameters:

  • saved (true, false)

    The value to set for new_record.

Returns:

  • (true, false)

    The new_record value.



29
30
31
# File 'lib/mongoid/state.rb', line 29

def new_record=(saved)
  @new_record = saved
end

#new_record?true, false Also known as: new?

Returns true if the Document has not been persisted to the database, false if it has. This is determined by the variable @new_record and NOT if the object has an id.

Examples:

Is the document new?

person.new_record?

Returns:

  • (true, false)

    True if new, false if not.



16
17
18
# File 'lib/mongoid/state.rb', line 16

def new_record?
  @new_record == true
end

#persisted?true, false

Checks if the document has been saved to the database. Returns false if the document has been destroyed.

Examples:

Is the document persisted?

person.persisted?

Returns:

  • (true, false)

    True if persisted, false if not.



40
41
42
# File 'lib/mongoid/state.rb', line 40

def persisted?
  !new_record? && !destroyed?
end