diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-05-14 11:37:34 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-05-14 11:37:34 -0700 |
commit | e8ddd4f9e5b7665d7569bfebd4c8a813f8781925 (patch) | |
tree | 21780d2a94027f8c3568067ed05354c959a05ce8 | |
parent | c5953f7dba20a6706a9f3dcce4cb43d7d91aad33 (diff) | |
download | rails-e8ddd4f9e5b7665d7569bfebd4c8a813f8781925.tar.gz rails-e8ddd4f9e5b7665d7569bfebd4c8a813f8781925.tar.bz2 rails-e8ddd4f9e5b7665d7569bfebd4c8a813f8781925.zip |
polymorphic after filter
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 95 |
1 files changed, 70 insertions, 25 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 0f6610825f..0ae69832bd 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -176,6 +176,75 @@ module ActiveSupport } end end + + class After + def self.build(next_callback, user_callback, user_conditions, chain_config) + if chain_config[:skip_after_callbacks_if_terminated] + if chain_config.key?(:terminator) && user_conditions.any? + halting_and_conditional(next_callback, user_callback, user_conditions) + elsif chain_config.key?(:terminator) + halting(next_callback, user_callback) + elsif user_conditions.any? + conditional next_callback, user_callback, user_conditions + else + simple next_callback, user_callback + end + else + if user_conditions.any? + conditional next_callback, user_callback, user_conditions + else + simple next_callback, user_callback + end + end + end + + private + + def self.halting_and_conditional(next_callback, user_callback, user_conditions) + lambda { |env| + env = next_callback.call env + target = env.target + value = env.value + halted = env.halted + + if !halted && user_conditions.all? { |c| c.call(target, value) } + user_callback.call target, value + end + env + } + end + + def self.halting(next_callback, user_callback) + lambda { |env| + env = next_callback.call env + if !env.halted + user_callback.call env.target, env.value + end + env + } + end + + def self.conditional(next_callback, user_callback, user_conditions) + lambda { |env| + env = next_callback.call env + target = env.target + value = env.value + + if user_conditions.all? { |c| c.call(target, value) } + user_callback.call target, value + end + env + } + end + + def self.simple(next_callback, user_callback) + lambda { |env| + env = next_callback.call env + user_callback.call env.target, env.value + env + } + end + end end class Callback #:nodoc:# @@ -248,31 +317,7 @@ module ActiveSupport when :before Filters::Before.build(next_callback, user_callback, user_conditions, chain_config, @filter) when :after - if chain_config[:skip_after_callbacks_if_terminated] - lambda { |env| - env = next_callback.call env - target = env.target - value = env.value - halted = env.halted - - if !halted && user_conditions.all? { |c| c.call(target, value) } - user_callback.call target, value - end - env - } - else - lambda { |env| - env = next_callback.call env - target = env.target - value = env.value - halted = env.halted - - if user_conditions.all? { |c| c.call(target, value) } - user_callback.call target, value - end - env - } - end + Filters::After.build(next_callback, user_callback, user_conditions, chain_config) when :around lambda { |env| target = env.target |