aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-05-14 11:20:34 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-05-14 11:20:34 -0700
commiteac50bc3359ca3dabfe5d8fea31c6a904362dbf3 (patch)
tree51949832eab5d0a4022a1fe9a9ea73fed8220487 /activesupport
parent9dcecbb080dca8e9525bcbc9a2d64ab4b417dd6d (diff)
downloadrails-eac50bc3359ca3dabfe5d8fea31c6a904362dbf3.tar.gz
rails-eac50bc3359ca3dabfe5d8fea31c6a904362dbf3.tar.bz2
rails-eac50bc3359ca3dabfe5d8fea31c6a904362dbf3.zip
polymorphic before callbacks
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/callbacks.rb70
1 files changed, 54 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 3be23c17e1..7ea77d5857 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -105,6 +105,59 @@ module ActiveSupport
end
end
ENDING = End.new
+
+ class Before
+ 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
+ }
+ elsif chain_config.key? :terminator
+ halted_lambda = eval "lambda { |result| #{chain_config[:terminator]} }"
+ 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
+ end
+ next_callback.call env
+ }
+ elsif user_conditions.any?
+ 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
+ }
+ else
+ lambda { |env|
+ user_callback.call env.target, env.value
+ next_callback.call env
+ }
+ end
+ end
+ end
end
class Callback #:nodoc:#
@@ -175,21 +228,7 @@ module ActiveSupport
case kind
when :before
- 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
- }
+ Filters::Before.build(next_callback, user_callback, user_conditions, chain_config, @filter)
when :after
if chain_config[:skip_after_callbacks_if_terminated]
lambda { |env|
@@ -335,7 +374,6 @@ module ActiveSupport
def initialize(name, config)
@name = name
@config = {
- :terminator => "false",
:scope => [ :kind ]
}.merge!(config)
@chain = []