diff options
Diffstat (limited to 'activesupport/lib/active_support/callbacks.rb')
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 66767bfbb6..e077cdbe4d 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -81,6 +81,14 @@ module ActiveSupport send("_run_#{kind}_callbacks", *args, &block) end + private + + # A hook invoked everytime a before callback is halted. + # This can be overriden in AS::Callback implementors in order + # to provide better debugging/logging. + def halted_callback_hook(filter) + end + class Callback #:nodoc:# @@_callback_sequence = 0 @@ -164,12 +172,14 @@ module ActiveSupport when :before <<-RUBY_EVAL if !halted && #{@compiled_options} - # This double assignment is to prevent warnings in 1.9.3. I would - # remove the `result` variable, but apparently some other - # generated code is depending on this variable being set sometimes - # and sometimes not. + # This double assignment is to prevent warnings in 1.9.3 as + # the `result` variable is not always used except if the + # terminator code refers to it. result = result = #{@filter} halted = (#{chain.config[:terminator]}) + if halted + halted_callback_hook(#{@raw_filter.inspect.inspect}) + end end #{code} RUBY_EVAL @@ -372,18 +382,11 @@ module ActiveSupport module ClassMethods # Generate the internal runner method called by +run_callbacks+. def __define_runner(symbol) #:nodoc: - body = send("_#{symbol}_callbacks").compile runner_method = "_run_#{symbol}_callbacks" - - silence_warnings do - undef_method runner_method if method_defined?(runner_method) + unless private_method_defined?(runner_method) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{runner_method}(key = nil, &blk) - if key - self.class.__run_keyed_callback(key, :#{symbol}, self, &blk) - else - #{body} - end + self.class.__run_callback(key, :#{symbol}, self, &blk) end private :#{runner_method} RUBY_EVAL @@ -394,10 +397,10 @@ module ActiveSupport # If this called first time it creates a new callback method for the key, # calculating which callbacks can be omitted because of per_key conditions. # - def __run_keyed_callback(key, kind, object, &blk) #:nodoc: - name = "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" + def __run_callback(key, kind, object, &blk) #:nodoc: + name = __callback_runner_name(key, kind) unless object.respond_to?(name) - str = send("_#{kind}_callbacks").compile(name, object) + str = send("_#{kind}_callbacks").compile(key, object) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{name}() #{str} end protected :#{name} @@ -406,6 +409,15 @@ module ActiveSupport object.send(name, &blk) end + def __reset_runner(symbol) + name = __callback_runner_name(nil, symbol) + undef_method(name) if method_defined?(name) + end + + def __callback_runner_name(key, kind) + "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" + end + # This is used internally to append, prepend and skip callbacks to the # CallbackChain. # @@ -417,7 +429,7 @@ module ActiveSupport ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| chain = target.send("_#{name}_callbacks") yield target, chain.dup, type, filters, options - target.__define_runner(name) + target.__reset_runner(name) end end @@ -531,12 +543,12 @@ module ActiveSupport chain = target.send("_#{symbol}_callbacks").dup callbacks.each { |c| chain.delete(c) } target.send("_#{symbol}_callbacks=", chain) - target.__define_runner(symbol) + target.__reset_runner(symbol) end self.send("_#{symbol}_callbacks=", callbacks.dup.clear) - __define_runner(symbol) + __reset_runner(symbol) end # Define sets of events in the object lifecycle that support callbacks. |