From b93cd60949bfc8d69b271e09eda0786b200e7a8b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 13 May 2013 14:23:03 -0700 Subject: extract getting an setting callbacks to methods --- activesupport/lib/active_support/callbacks.rb | 37 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 8ac49ec4b6..fc63fb8c8e 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -448,7 +448,7 @@ module ActiveSupport # CallbackChain. def __update_callbacks(name) #:nodoc: ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| - chain = target.send("_#{name}_callbacks") + chain = target.get_callbacks name yield target, chain.dup target.__reset_runner(name) end @@ -490,18 +490,15 @@ module ActiveSupport # * :prepend - If +true+, the callback will be prepended to the # existing chain rather than appended. def set_callback(name, *filter_list, &block) - mapped = nil - type, filters, options = normalize_callback_params(name, filter_list, block) + chain = get_callbacks name + mapped = filters.map do |filter| + Callback.build(chain, filter, type, options.dup, self) + end __update_callbacks(name) do |target, chain| - mapped ||= filters.map do |filter| - Callback.build(chain, filter, type, options.dup, self) - end - options[:prepend] ? chain.prepend(*mapped) : chain.append(*mapped) - - target.send("_#{name}_callbacks=", chain) + target.set_callbacks name, chain end end @@ -527,22 +524,22 @@ module ActiveSupport chain.delete(filter) end - target.send("_#{name}_callbacks=", chain) + target.set_callbacks name, chain end end # Remove all set callbacks for the given event. def reset_callbacks(symbol) - callbacks = send("_#{symbol}_callbacks") + callbacks = get_callbacks symbol ActiveSupport::DescendantsTracker.descendants(self).each do |target| - chain = target.send("_#{symbol}_callbacks").dup + chain = target.get_callbacks(symbol).dup callbacks.each { |c| chain.delete(c) } - target.send("_#{symbol}_callbacks=", chain) + target.set_callbacks symbol, chain target.__reset_runner(symbol) end - self.send("_#{symbol}_callbacks=", callbacks.dup.clear) + self.set_callbacks symbol, callbacks.dup.clear __reset_runner(symbol) end @@ -618,9 +615,19 @@ module ActiveSupport config = callbacks.last.is_a?(Hash) ? callbacks.pop : {} callbacks.each do |callback| class_attribute "_#{callback}_callbacks" - send("_#{callback}_callbacks=", CallbackChain.new(callback, config)) + set_callbacks callback, CallbackChain.new(callback, config) end end + + protected + + def get_callbacks(name) + send "_#{name}_callbacks" + end + + def set_callbacks(name, callbacks) + send "_#{name}_callbacks=", callbacks + end end end end -- cgit v1.2.3