module Mongoid::Document
def _destroy
only supports immediate deletion of associations.
destruction of this association. Always returns false because Mongoid
Used in conjunction with fields_for to build a form element for the
def _destroy false end
def as_document
- Since: - 1.0.0
Returns:
-
(Hash)- A hash of all attributes in the hierarchy.
Other tags:
- Example: Get the full hierarchy. -
def as_document return attributes if frozen? embedded_relations.each_pair do |name, meta| without_autobuild do relation, stored = send(name), meta.store_as if attributes.key?(stored) || !relation.blank? if relation attributes[stored] = relation.as_document else attributes.delete(stored) end end end end attributes end
def as_json(options = nil)
- Since: - 5.1.0
Returns:
-
(Hash)- The document as json.
Options Hash:
(**options)-
:compact(true, false) -- Whether to include fields with
Parameters:
-
options(Hash) -- The options.
Other tags:
- Example: Get the document as json. -
def as_json(options = nil) if options && (options[:compact] == true) super(options).reject! { |k,v| v.nil? } else super(options) end end
def becomes(klass)
- Since: - 2.0.0
Returns:
-
(Document)- An instance of the specified class.
Parameters:
-
klass(Class) -- The class to become.
Raises:
-
(ArgumentError)- If the class doesn't include Mongoid::Document
Other tags:
- Example: Return a subclass document as a superclass instance. -
def becomes(klass) unless klass.include?(Mongoid::Document) raise ArgumentError, "A class which includes Mongoid::Document is expected" end became = klass.new(clone_document) became._id = _id became.instance_variable_set(:@changed_attributes, changed_attributes) became.instance_variable_set(:@errors, ActiveModel::Errors.new(became)) became.errors.instance_variable_set(:@messages, errors.instance_variable_get(:@messages)) became.instance_variable_set(:@new_record, new_record?) became.instance_variable_set(:@destroyed, destroyed?) became.changed_attributes["_type"] = self.class.to_s became._type = klass.to_s # mark embedded docs as persisted embedded_relations.each_pair do |name, meta| without_autobuild do relation = became.__send__(name) Array.wrap(relation).each do |r| r.instance_variable_set(:@new_record, new_record?) end end end became end
def cache_key
- Since: - 2.4.0
Returns:
-
(String)- the string with or without updated_at
Other tags:
- Example: Returns the cache key -
def cache_key return "#{model_key}/new" if new_record? return "#{model_key}/#{id}-#{updated_at.utc.to_s(:nsec)}" if do_or_do_not(:updated_at) "#{model_key}/#{id}" end
def freeze
- Since: - 2.0.0
Returns:
-
(Document)- The document.
Other tags:
- Example: Freeze the document -
def freeze as_document.freeze and self end
def frozen?
- Since: - 2.0.0
Returns:
-
(true, false)- True if frozen, else false.
Other tags:
- Example: Check if frozen -
def frozen? attributes.frozen? end
def hash
- Since: - 1.0.0
Returns:
-
(Integer)- The hash of the document's identity.
Other tags:
- Example: Get the hash. -
def hash identity.hash end
def identity
- Since: - 3.0.0
Returns:
-
(Array)- An array containing [document.class, document._id]
Other tags:
- Example: Get the identity -
def identity [ self.class, self._id ] end
def initialize(attrs = nil)
- Since: - 1.0.0
Returns:
-
(Document)- A new document.
Parameters:
-
attrs(Hash) -- The attributes to set up the document with.
Other tags:
- Example: Create a new document. -
def initialize(attrs = nil) _building do @new_record = true @attributes ||= {} with(self.class.persistence_options) apply_pre_processed_defaults apply_default_scoping process_attributes(attrs) do yield(self) if block_given? end apply_post_processed_defaults # @todo: #2586: Need to have access to parent document in these # callbacks. run_callbacks(:initialize) unless _initialize_callbacks.empty? end end
def logger
- Since: - 2.2.0
Returns:
-
(Logger)- The configured logger or a default Logger instance.
def logger Mongoid.logger end
def model_key
- Since: - 2.4.0
Returns:
-
(String)- The model key.
Other tags:
- Example: Get the model key. -
def model_key @model_cache_key ||= self.class.model_name.cache_key end
def model_name
- Since: - 3.0.16
Returns:
-
(String)- The model name.
Other tags:
- Example: Return the model name. -
def model_name self.class.model_name end
def to_a
- Since: - 1.0.0
Returns:
-
(Array- An array with the document as its only item.)
Other tags:
- Example: Return the document in an array. -
def to_a [ self ] end
def to_ary
- Since: - 2.1.0
Returns:
-
(nil)- Always nil.
Other tags:
- Example: Get the document as an array. -
def to_ary nil end
def to_key
- Since: - 2.4.0
Returns:
-
(String)- The id of the document or nil if new.
Other tags:
- Example: Return the key. -
def to_key (persisted? || destroyed?) ? [ id.to_s ] : nil end