diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-05-14 11:27:24 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-05-14 11:27:24 -0700 |
commit | bd95ff84f3199fba2c35c2727182672ac75c08b2 (patch) | |
tree | 36916f522729bf98456875c708b4a560c4cf8a18 /activesupport/lib | |
parent | eac50bc3359ca3dabfe5d8fea31c6a904362dbf3 (diff) | |
download | rails-bd95ff84f3199fba2c35c2727182672ac75c08b2.tar.gz rails-bd95ff84f3199fba2c35c2727182672ac75c08b2.tar.bz2 rails-bd95ff84f3199fba2c35c2727182672ac75c08b2.zip |
push the before filter lambdas to factory methods
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 7ea77d5857..d0c3f4bde9 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -110,52 +110,70 @@ module ActiveSupport def self.build(next_callback, user_callback, user_conditions, chain_config, filter) if chain_config.key?(:terminator) && user_conditions.any? halted_lambda = eval "lambda { |result| #{chain_config[:terminator]} }" - lambda { |env| - target = env.target - value = env.value - halted = env.halted - - if !halted && user_conditions.all? { |c| c.call(target, value) } - result = user_callback.call target, value - env.halted = halted_lambda.call result - if env.halted - target.send :halted_callback_hook, filter - end - end - next_callback.call env - } + terminal_and_conditional(next_callback, user_callback, user_conditions, halted_lambda, filter) elsif chain_config.key? :terminator halted_lambda = eval "lambda { |result| #{chain_config[:terminator]} }" - lambda { |env| - target = env.target - value = env.value - halted = env.halted + terminal(next_callback, user_callback, halted_lambda, filter) + elsif user_conditions.any? + conditional(next_callback, user_callback, user_conditions) + else + simple next_callback, user_callback + end + end + + private + + def self.terminal_and_conditional(next_callback, user_callback, user_conditions, halted_lambda, filter) + lambda { |env| + target = env.target + value = env.value + halted = env.halted - if !halted - result = user_callback.call target, value - env.halted = halted_lambda.call result - if env.halted - target.send :halted_callback_hook, filter - end + if !halted && user_conditions.all? { |c| c.call(target, value) } + result = user_callback.call target, value + env.halted = halted_lambda.call result + if env.halted + target.send :halted_callback_hook, filter end - next_callback.call env - } - elsif user_conditions.any? - lambda { |env| - target = env.target - value = env.value + end + next_callback.call env + } + end - if user_conditions.all? { |c| c.call(target, value) } - user_callback.call target, value + def self.terminal(next_callback, user_callback, halted_lambda, filter) + lambda { |env| + target = env.target + value = env.value + halted = env.halted + + if !halted + result = user_callback.call target, value + env.halted = halted_lambda.call result + if env.halted + target.send :halted_callback_hook, filter end - next_callback.call env - } - else - lambda { |env| - user_callback.call env.target, env.value - next_callback.call env - } - end + end + next_callback.call env + } + end + + def self.conditional(next_callback, user_callback, user_conditions) + lambda { |env| + target = env.target + value = env.value + + if user_conditions.all? { |c| c.call(target, value) } + user_callback.call target, value + end + next_callback.call env + } + end + + def self.simple(next_callback, user_callback) + lambda { |env| + user_callback.call env.target, env.value + next_callback.call env + } end end end |