Module: Mongoid::Stateful

Included in:
Composable
Defined in:
lib/mongoid/stateful.rb

Overview

Mixin module included into Mongoid::Document which adds behavior for getting the various lifecycle states a document can transition through.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destroyed=(value) ⇒ Object (writeonly)

Sets the attribute destroyed

Parameters:

  • value

    the value to set the attribute destroyed to.



7
8
9
# File 'lib/mongoid/stateful.rb', line 7

def destroyed=(value)
  @destroyed = value
end

#flagged_for_destroy=(value) ⇒ Object (writeonly)

Sets the attribute flagged_for_destroy

Parameters:

  • value

    the value to set the attribute flagged_for_destroy to.



7
8
9
# File 'lib/mongoid/stateful.rb', line 7

def flagged_for_destroy=(value)
  @flagged_for_destroy = value
end

#previously_new_record=(value) ⇒ Object (writeonly)

Sets the attribute previously_new_record

Parameters:

  • value

    the value to set the attribute previously_new_record to.



7
8
9
# File 'lib/mongoid/stateful.rb', line 7

def previously_new_record=(value)
  @previously_new_record = value
end

Instance Method Details

#destroyed?true | false

Returns true if the Document has been successfully 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.



82
83
84
# File 'lib/mongoid/stateful.rb', line 82

def destroyed?
  @destroyed ||= false
end

#flagged_for_destroy?true | false Also known as: marked_for_destruction?, _destroy

Returns whether or not the document has been flagged for deletion, but not destroyed yet. Used for atomic pulls of child documents.

Examples:

Is the document flagged?

document.flagged_for_destroy?

Returns:

  • (true | false)

    If the document is flagged.



68
69
70
# File 'lib/mongoid/stateful.rb', line 68

def flagged_for_destroy?
  @flagged_for_destroy ||= false
end

#new_record=(new_value) ⇒ true | false

Sets whether the document has been persisted to the database.

Parameters:

  • new_value (true | false)

    The value to set.

Returns:

  • (true | false)

    The set value.



14
15
16
17
18
# File 'lib/mongoid/stateful.rb', line 14

def new_record=(new_value)
  @new_record ||= false
  @previously_new_record = true if @new_record && !new_value
  @new_record = new_value
end

#new_record?true | false

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.



28
29
30
# File 'lib/mongoid/stateful.rb', line 28

def new_record?
  @new_record ||= false
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.



48
49
50
# File 'lib/mongoid/stateful.rb', line 48

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

#previously_new_record?true | false

Returns true if this document was just created -- that is, prior to the last save, the object didn't exist in the database and new_record? would have returned true.

Returns:

  • (true | false)

    True if was just created, false if not.



37
38
39
# File 'lib/mongoid/stateful.rb', line 37

def previously_new_record?
  @previously_new_record ||= false
end

#previously_persisted?true | false

Checks if the document was previously saved to the database but now it has been deleted.

Returns:

  • (true | false)

    True if was persisted but now destroyed, otherwise false.



57
58
59
# File 'lib/mongoid/stateful.rb', line 57

def previously_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?



92
93
94
95
96
97
# File 'lib/mongoid/stateful.rb', line 92

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

#readonly!true | false

Flags the document as readonly. Will cause a ReadonlyDocument error to be raised if the document is attempted to be saved, updated or destroyed.

Examples:

Flag the document as readonly.

document.readonly!

Returns:

  • (true | false)

    true if the document was successfully marked readonly, false otherwise.



107
108
109
110
111
112
113
114
# File 'lib/mongoid/stateful.rb', line 107

def readonly!
  if Mongoid.legacy_readonly
    Mongoid::Warnings.warn_legacy_readonly
    false
  else
    @readonly = true
  end
end

#readonly?true | false

Is the document readonly?

Examples:

Is the document readonly?

document.readonly?

Returns:

  • (true | false)

    If the document is readonly.



122
123
124
125
126
127
128
# File 'lib/mongoid/stateful.rb', line 122

def readonly?
  if Mongoid.legacy_readonly
    !__selected_fields.nil?
  else
    @readonly ||= false
  end
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?



136
137
138
# File 'lib/mongoid/stateful.rb', line 136

def settable?
  new_record? && 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.



146
147
148
# File 'lib/mongoid/stateful.rb', line 146

def updateable?
  persisted? && changed?
end