Sha256: aba8d1dca8b39b58de4ab2fef7e7a8d40a5b313eeab4d399d3a023a9b9ef33a2

Contents?: true

Size: 1.45 KB

Versions: 34

Compression:

Stored size: 1.45 KB

Contents

module InterceptableSpec
  class CallbackRegistry
    def initialize
      @calls = []
    end

    def record_call(cls, cb)
      @calls << [cls, cb]
    end

    attr_reader :calls
  end

  module CallbackTracking
    extend ActiveSupport::Concern

    included do
      %i(
        validation save create update
      ).each do |what|
        %i(before after).each do |whn|
          send("#{whn}_#{what}", "#{whn}_#{what}_stub".to_sym)
          define_method("#{whn}_#{what}_stub") do
            callback_registry.record_call(self.class, "#{whn}_#{what}".to_sym)
          end
        end
      end
    end
  end

  class CbParent
    include Mongoid::Document

    def initialize(callback_registry)
      @callback_registry = callback_registry
      super()
    end

    attr_reader :callback_registry

    embeds_many :cb_children
    embeds_many :cb_cascaded_children, cascade_callbacks: true

    include CallbackTracking
  end

  class CbChild
    include Mongoid::Document

    embedded_in :cb_parent

    def initialize(callback_registry, options)
      @callback_registry = callback_registry
      super(options)
    end

    attr_reader :callback_registry

    include CallbackTracking
  end

  class CbCascadedChild
    include Mongoid::Document

    embedded_in :cb_parent

    def initialize(callback_registry, options)
      @callback_registry = callback_registry
      super(options)
    end

    attr_reader :callback_registry

    include CallbackTracking
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
mongoid-7.5.4 spec/mongoid/interceptable_spec_models.rb
mongoid-7.5.3 spec/mongoid/interceptable_spec_models.rb
mongoid-7.5.2 spec/mongoid/interceptable_spec_models.rb
mongoid-7.5.1 spec/mongoid/interceptable_spec_models.rb
mongoid-7.4.3 spec/mongoid/interceptable_spec_models.rb
mongoid-7.5.0 spec/mongoid/interceptable_spec_models.rb
mongoid-7.4.1 spec/mongoid/interceptable_spec_models.rb
mongoid-7.3.5 spec/mongoid/interceptable_spec_models.rb
mongoid-7.4.0 spec/mongoid/interceptable_spec_models.rb
mongoid-7.3.4 spec/mongoid/interceptable_spec_models.rb
mongoid-7.1.11 spec/mongoid/interceptable_spec_models.rb
mongoid-7.2.6 spec/mongoid/interceptable_spec_models.rb
mongoid-7.3.3 spec/mongoid/interceptable_spec_models.rb
mongoid-7.3.2 spec/mongoid/interceptable_spec_models.rb
mongoid-7.2.5 spec/mongoid/interceptable_spec_models.rb
mongoid-7.1.10 spec/mongoid/interceptable_spec_models.rb
mongoid-7.1.9 spec/mongoid/interceptable_spec_models.rb
mongoid-7.2.4 spec/mongoid/interceptable_spec_models.rb
mongoid-7.3.1 spec/mongoid/interceptable_spec_models.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/mongoid/interceptable_spec_models.rb