Sha256: 98067676276ea936d9dc492a69b6e62627ffb919bd709f4ed39b0cd11a2b34c5

Contents?: true

Size: 932 Bytes

Versions: 3

Compression:

Stored size: 932 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  class Observer < ActiveModel::Observer
    def initialize
      super
      observed_descendants.each { |klass| add_observer!(klass) }
    end

    protected

    def observed_descendants
      observed_classes.sum([]) { |klass| klass.descendants }
    end

    def add_observer!(klass)
      super
      define_callbacks klass
    end

    def define_callbacks(klass)
      observer = self
      observer_name = observer.class.name.underscore.gsub('/', '__')

      Mongoid::Callbacks::CALLBACKS.each do |callback|
        next unless respond_to?(callback)
        callback_meth = :"_notify_#{observer_name}_for_#{callback}"
        unless klass.respond_to?(callback_meth)
          klass.send(:define_method, callback_meth) do |&block|
            observer.send(callback, self, &block)
          end
          klass.send(callback, callback_meth)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-2.0.1 lib/mongoid/observer.rb
mongoid-2.0.0 lib/mongoid/observer.rb
mongoid-2.0.0.rc.8 lib/mongoid/observer.rb