Sha256: 028f61048c8864ecbba5ac9e4f7ee6d28a89f9281a5b5626d9949c02ac138701

Contents?: true

Size: 1.57 KB

Versions: 59

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
require "mongoid/relations/cascading/delete"
require "mongoid/relations/cascading/destroy"
require "mongoid/relations/cascading/nullify"
require "mongoid/relations/cascading/restrict"

module Mongoid
  module Relations

    # This module defines the behaviour for setting up cascading deletes and
    # nullifies for relations, and how to delegate to the approriate strategy.
    module Cascading
      extend ActiveSupport::Concern

      included do
        class_attribute :cascades
        self.cascades = []
      end

      # Perform all cascading deletes, destroys, or nullifies. Will delegate to
      # the appropriate strategy to perform the operation.
      #
      # @example Execute cascades.
      #   document.cascade!
      #
      # @since 2.0.0.rc.1
      def cascade!
        cascades.each do |name|
          if meta = relations[name]
            strategy = meta.cascade_strategy
            strategy.new(self, meta).cascade if strategy
          end
        end
      end

      module ClassMethods

        # Attempt to add the cascading information for the document to know how
        # to handle associated documents on a removal.
        #
        # @example Set up cascading information
        #   Movie.cascade(metadata)
        #
        # @param [ Metadata ] metadata The metadata for the relation.
        #
        # @return [ Class ] The class of the document.
        #
        # @since 2.0.0.rc.1
        def cascade(metadata)
          cascades.push(metadata.name.to_s) if metadata.dependent?
          self
        end
      end
    end
  end
end

Version data entries

59 entries across 54 versions & 6 rubygems

Version Path
mongoid-6.4.8 lib/mongoid/relations/cascading.rb
mongoid-6.4.7 lib/mongoid/relations/cascading.rb
mongoid-6.4.5 lib/mongoid/relations/cascading.rb
mongoid-5.4.1 lib/mongoid/relations/cascading.rb
mongoid-6.4.4 lib/mongoid/relations/cascading.rb
mongoid-6.4.2 lib/mongoid/relations/cascading.rb
mongoid-6.4.1 lib/mongoid/relations/cascading.rb
mongoid-5.4.0 lib/mongoid/relations/cascading.rb
tdiary-5.0.8 vendor/bundle/gems/mongoid-6.4.0/lib/mongoid/relations/cascading.rb
mongoid-6.4.0 lib/mongoid/relations/cascading.rb
mongoid-6.3.0 lib/mongoid/relations/cascading.rb
mongoid-6.2.1 lib/mongoid/relations/cascading.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.2.0/lib/mongoid/relations/cascading.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/cascading.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/cascading.rb
mongoid-6.2.0 lib/mongoid/relations/cascading.rb
mongoid-6.1.1 lib/mongoid/relations/cascading.rb
mongoid-5.2.1 lib/mongoid/relations/cascading.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/relations/cascading.rb
mongoid-6.1.0 lib/mongoid/relations/cascading.rb