Sha256: 1868b90a98d9d446f68442c0fabfe62a2fcdced8a0d164bd880ff7f75bee38cd

Contents?: true

Size: 1.25 KB

Versions: 91

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
module Mongoid
  module Relations
    module Cascading
      class Destroy

        attr_accessor :document, :relation, :metadata

        # Initialize the new cascade strategy, which will set up the relation
        # and the metadata.
        #
        # @example Instantiate the strategy
        #   Strategy.new(document, metadata)
        #
        # @param [ Document ] document The document to cascade from.
        # @param [ Metadata ] metadata The relation's metadata.
        #
        # @return [ Strategy ] The new strategy.
        def initialize(document, metadata)
          @document, @metadata = document, metadata
          @relation = document.send(metadata.name)
        end

        # Execute the cascading deletion for the relation if it already exists.
        # This should be optimized in the future potentially not to load all
        # objects from the db.
        #
        # @example Perform the cascading destroy.
        #   strategy.cascade
        def cascade
          if relation
            if relation.is_a?(Enumerable)
              relation.entries
              relation.each { |doc| doc.destroy }
            else
              relation.destroy
            end
          end
        end
      end
    end
  end
end

Version data entries

91 entries across 86 versions & 7 rubygems

Version Path
mongoid-5.2.0 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.2.0.rc0 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.1.0.rc0 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.3 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.6 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.2 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.1 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.5 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.0 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.4 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.0.rc0 lib/mongoid/relations/cascading/destroy.rb
mongoid-6.0.0.beta lib/mongoid/relations/cascading/destroy.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/relations/cascading/destroy.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-5.1.3/lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.3 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.2 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.1 lib/mongoid/relations/cascading/destroy.rb
mongoid-5.1.0 lib/mongoid/relations/cascading/destroy.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/relations/cascading/destroy.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/relations/cascading/destroy.rb