Permalink
Browse files

Merge branch 'embedded_callbacks_fix'

  • Loading branch information...
cheald committed Jul 3, 2013
2 parents 4699250 + 8b637c2 commit 8e6cd50eac2e180978f556e269ded2255f23e31b
Showing with 35 additions and 10 deletions.
  1. +35 −10 lib/mongo_mapper/plugins/embedded_callbacks.rb
@@ -10,10 +10,20 @@ module EmbeddedCallbacks
define_model_callbacks :save, :create, :update, :destroy, :only => [:before, :after]
define_model_callbacks :touch, :only => [:after]

embedded_callbacks_on
proxy_callbacks(
:before => [:save, :create, :update, :destroy],
:after => [:save, :create, :update, :destroy, :touch]
)

@embedded_callbacks_status = nil
end

module ClassMethods
def define_callbacks(*args)
embedded_callbacks_on if @embedded_callbacks_status.nil?
super
end

def embedded_callbacks_on
@embedded_callbacks_status = true
end
@@ -23,22 +33,37 @@ def embedded_callbacks_off
end

def embedded_callbacks_on?
@embedded_callbacks_status == true
!!@embedded_callbacks_status
end

def embedded_callbacks_off?
!embedded_callbacks_on?
!@embedded_callbacks_status
end
end

def run_callbacks(callback, *args, &block)

if self.class.embedded_callbacks_on?
embedded_docs = []
def proxy_callbacks(definition)
definition.each do |prefix, suffixes|
suffixes.each do |suffix|
callback = "%s_%s" % [prefix, suffix]
class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
class << self
alias_method :__original_#{callback}, :#{callback}
embedded_associations.each do |association|
embedded_docs += Array(get_proxy(association).send(:load_target))
def #{callback}(*args, &block)
embedded_callbacks_on if @embedded_callbacks_status.nil?
__original_#{callback}(*args, &block)
end
end
CALLBACK
end
end
end
end

def run_callbacks(callback, *args, &block)
if self.class.embedded_callbacks_on? and embedded_associations.length > 0
embedded_docs = embedded_associations.map do |association|
Array(get_proxy(association).send(:load_target))
end.flatten(1)

block = embedded_docs.inject(block) do |chain, doc|
if doc.class.respond_to?("_#{callback}_callbacks")

0 comments on commit 8e6cd50

Please sign in to comment.