aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-05-13 14:23:03 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-05-13 14:23:03 -0700
commitb93cd60949bfc8d69b271e09eda0786b200e7a8b (patch)
treeb68395f0f2be3b8590b4dbf6a1302e4e33626f8d /activesupport
parent72be280ffe1f548fcdbff5f85b69b47c230722a7 (diff)
downloadrails-b93cd60949bfc8d69b271e09eda0786b200e7a8b.tar.gz
rails-b93cd60949bfc8d69b271e09eda0786b200e7a8b.tar.bz2
rails-b93cd60949bfc8d69b271e09eda0786b200e7a8b.zip
extract getting an setting callbacks to methods
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/callbacks.rb37
1 files changed, 22 insertions, 15 deletions
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
# * <tt>:prepend</tt> - 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