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

#pushable?true, false

Determine if the document can be pushed.

Examples:

Is this pushable?

person.pushable?

Returns:

  • (true, false)

    Is the document new and embedded?



73
74
75
76
77
78
# File 'lib/mongoid/state.rb', line 73

def pushable?
  new? &&
    embedded_many? &&
    _parent.persisted? &&
    !_parent.delayed_atomic_sets[atomic_path]
end

#settable?true, false

Determine if the document can be set.

Examples:

Is this settable?

person.settable?

Returns:

  • (true, false)

    Is this document a new embeds one?

Since:

  • 2.1.0



88
89
90
# File 'lib/mongoid/state.rb', line 88

def settable?
  new? && embedded_one? && _parent.persisted?
end

#updateable?true, false

Is the document updateable?

Examples:

Is the document updateable?

person.updateable?

Returns:

  • (true, false)

    If the document is changed and persisted.

Since:

  • 2.1.0



100
101
102
# File 'lib/mongoid/state.rb', line 100

def updateable?
  persisted? && changed?
end