Module: MongoMapper::Plugins::EmbeddedCallbacks

Extended by:
ActiveSupport::Concern
Included in:
Document, EmbeddedDocument
Defined in:
lib/mongo_mapper/plugins/embedded_callbacks.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#run_callbacks(callback, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongo_mapper/plugins/embedded_callbacks.rb', line 34

def run_callbacks(callback, *args, &block)

  if self.class.embedded_callbacks_on?
    embedded_docs = []

    embedded_associations.each do |association|
      embedded_docs += Array(get_proxy(association).send(:load_target))
    end

    block = embedded_docs.inject(block) do |chain, doc|
      if doc.class.respond_to?("_#{callback}_callbacks")
        lambda { doc.run_callbacks(callback, *args, &chain) }
      else
        chain
      end
    end
  end

  super callback, *args, &block
end