Sha256: 3afa977c43b3b7b89091910c772151fc423ad0a4bea4eef2f3376f86db03c5d9

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 Delete

        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 delete.
        #   strategy.cascade
        #
        # @since 2.0.0
        def cascade
          if relation
            if relation.cascades.empty?
              relation.clear
            else
              ::Array.wrap(relation).each { |doc| doc.delete }
            end
          end
        end
      end
    end
  end
end

Version data entries

91 entries across 86 versions & 7 rubygems

Version Path
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/relations/cascading/delete.rb
mongoid-5.0.2 lib/mongoid/relations/cascading/delete.rb
mongoid-5.0.1 lib/mongoid/relations/cascading/delete.rb
mongoid-5.0.0 lib/mongoid/relations/cascading/delete.rb
mongoid-5.0.0.rc0 lib/mongoid/relations/cascading/delete.rb
mongoid-5.0.0.beta lib/mongoid/relations/cascading/delete.rb
mongoid-3.1.7 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.2 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.1 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.rc2 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.rc1 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.beta2 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.beta1 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.alpha2 lib/mongoid/relations/cascading/delete.rb
mongoid-4.0.0.alpha1 lib/mongoid/relations/cascading/delete.rb
mongoid-3.1.6 lib/mongoid/relations/cascading/delete.rb
sepastian-mongoid-rails4-4.0.1.alpha lib/mongoid/relations/cascading/delete.rb
sepastian-mongoid-rails4-4.0.0.alpha lib/mongoid/relations/cascading/delete.rb
mongoid-3.1.5 lib/mongoid/relations/cascading/delete.rb