Sha256: 86cff9d5f10958ff539e68f9e8c852986da9aaeb3ddd01df931bf04e3437fe2a

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 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
      # Insert the new document in the database. If the document's parent is a
      # new record, we will call save on the parent, otherwise we will $push
      # the document onto the parent.
      #
      # 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(@document)
        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

14 entries across 14 versions & 6 rubygems

Version Path
mongoid-1.9.5 lib/mongoid/persistence/remove_embedded.rb
mongoid-with-auth-1.9.4 lib/mongoid/persistence/remove_embedded.rb
mongoid-rails2-1.9.4 lib/mongoid/persistence/remove_embedded.rb
mongoid-rails2-1.9.3 lib/mongoid/persistence/remove_embedded.rb
mongoid-1.9.2 lib/mongoid/persistence/remove_embedded.rb
sskirby-mongoid-1.9.1 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.0.beta.7 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.0.beta.8 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.0.beta.9 lib/mongoid/persistence/remove_embedded.rb
mongoid-locomotive-2.0.0.beta9 lib/mongoid/persistence/remove_embedded.rb
mongoid-1.9.1 lib/mongoid/persistence/remove_embedded.rb
chhean-mongoid-2.0.1.beta1 lib/mongoid/persistence/remove_embedded.rb
mongoid-2.0.0.beta.5 lib/mongoid/persistence/remove_embedded.rb
mongoid-1.9.0 lib/mongoid/persistence/remove_embedded.rb