Sha256: d02a188cbfac2ae64c90069b7013c96820de2520789b37e8df483246d48d394f

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

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

module Mongoid # :nodoc:
  module Relations #:nodoc:

    # 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_inheritable_accessor :cascades
        self.cascades = []
        delegate :cascades, :to => "self.class"
      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|
          metadata = relations[name]
          strategy = metadata.cascade_strategy
          strategy.new(self, metadata).cascade
        end
      end

      module ClassMethods #:nodoc:

        # 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)
          tap { cascades << metadata.name.to_s if metadata.dependent? }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-2.0.0.rc.5 lib/mongoid/relations/cascading.rb
mongoid-2.0.0.rc.4 lib/mongoid/relations/cascading.rb
mongoid-2.0.0.rc.3 lib/mongoid/relations/cascading.rb
mongoid-2.0.0.rc.2 lib/mongoid/relations/cascading.rb
mongoid-2.0.0.rc.1 lib/mongoid/relations/cascading.rb