module Mongoid::Stateful

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

Attributes

destroyed[W]
flagged_for_destroy[W]
new_record[W]

Public Instance Methods

_destroy()
destroyed?() click to toggle source

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.

@example Is the document destroyed?

person.destroyed?

@return [ true, false ] True if destroyed, false if not.

# File lib/mongoid/stateful.rb, line 58
def destroyed?
  @destroyed ||= false
end
flagged_for_destroy?() click to toggle source

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

@example Is the document flagged?

document.flagged_for_destroy?

@return [ true, false ] If the document is flagged.

@since 2.3.2

# File lib/mongoid/stateful.rb, line 44
def flagged_for_destroy?
  @flagged_for_destroy ||= false
end
Also aliased as: marked_for_destruction?, _destroy
marked_for_destruction?()
new_record?() click to toggle source

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.

@example Is the document new?

person.new_record?

@return [ true, false ] True if new, false if not.

# File lib/mongoid/stateful.rb, line 20
def new_record?
  @new_record ||= false
end
persisted?() click to toggle source

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

@example Is the document persisted?

person.persisted?

@return [ true, false ] True if persisted, false if not.

# File lib/mongoid/stateful.rb, line 31
def persisted?
  !new_record? && !destroyed?
end
pushable?() click to toggle source

Determine if the document can be pushed.

@example Is this pushable?

person.pushable?

@return [ true, false ] Is the document new and embedded?

# File lib/mongoid/stateful.rb, line 68
def pushable?
  new_record? &&
    embedded_many? &&
    _parent.persisted? &&
    !_parent.delayed_atomic_sets[atomic_path]
end
readonly?() click to toggle source

Is the document readonly?

@example Is the document readonly?

document.readonly?

@return [ true, false ] If the document is readonly.

@since 4.0.0

# File lib/mongoid/stateful.rb, line 83
def readonly?
  __selected_fields != nil
end
settable?() click to toggle source

Determine if the document can be set.

@example Is this settable?

person.settable?

@return [ true, false ] Is this document a new embeds one?

@since 2.1.0

# File lib/mongoid/stateful.rb, line 95
def settable?
  new_record? && embedded_one? && _parent.persisted?
end
updateable?() click to toggle source

Is the document updateable?

@example Is the document updateable?

person.updateable?

@return [ true, false ] If the document is changed and persisted.

@since 2.1.0

# File lib/mongoid/stateful.rb, line 107
def updateable?
  persisted? && changed?
end

Private Instance Methods

reset_readonly() click to toggle source
# File lib/mongoid/stateful.rb, line 113
def reset_readonly
  self.__selected_fields = nil
end