Sha256: 9137926dac41f33b75387f567f0bc9f2e17b7131e344d28eaee9c38e51fa3d96

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Persistence #:nodoc:

    # Remove is a persistence command responsible for deleting a document from
    # the database.
    #
    # The underlying query resembles the following MongoDB query:
    #
    #   collection.remove(
    #     { "_id" : 1 },
    #     false
    #   );
    class RemoveEmbedded < Command

      # Remove the document from the database. If the parent is a new record,
      # it will get removed in Ruby only. If the parent is not a new record
      # then either an $unset or $set will occur, depending if it's an
      # embeds_one or embeds_many.
      #
      # Example:
      #
      # <tt>RemoveEmbedded.persist</tt>
      #
      # Returns:
      #
      # +true+ or +false+, depending on if the removal passed.
      def persist
        parent = document._parent
        parent.remove_child(document) unless suppress?
        unless parent.new_record?
          update = { document._remover => removal_selector }
          collection.update(parent._selector, update, options.merge(:multi => false))
        end; true
      end

      protected
      # Get the value to pass to the removal modifier.
      def setter
        document._index ? document.id : true
      end

      def removal_selector
        document._index ? { document._pull => { "_id" => document.id } } : { document._path => setter }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
mongoid-braxton-2.0.2 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.2 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.1 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.0 lib/mongoid/persistence/remove_embedded.rb