Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3637 from bogdan/compile_options

AS::Callbacks::Callback refactor
  • Loading branch information...
commit 0e8dda7eabb92384fb107b42339430f058166b7d 2 parents be52f1b + 3dc80b7
@josevalim josevalim authored
Showing with 62 additions and 58 deletions.
  1. +62 −58 activesupport/lib/active_support/callbacks.rb
View
120 activesupport/lib/active_support/callbacks.rb
@@ -166,19 +166,10 @@ def _one_time_conditions_valid_#{@callback_id}?
RUBY_EVAL
end
- # This will supply contents for before and around filters, and no
- # contents for after filters (for the forward pass).
- def start(key=nil, object=nil)
- return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")
-
- # options[0] is the compiled form of supplied conditions
- # options[1] is the "end" for the conditional
- #
+ # Wraps code with filter
+ def apply(code, key=nil, object=nil)
case @kind
when :before
- # if condition # before_save :filter_name, :if => :condition
- # filter_name
- # end
<<-RUBY_EVAL
if !halted && #{@compiled_options}
# This double assignment is to prevent warnings in 1.9.3 as
@@ -190,62 +181,64 @@ def start(key=nil, object=nil)
halted_callback_hook(#{@raw_filter.inspect.inspect})
end
end
+ #{code}
RUBY_EVAL
- when :around
- # Compile around filters with conditions into proxy methods
- # that contain the conditions.
- #
- # For `around_save :filter_name, :if => :condition':
- #
- # def _conditional_callback_save_17
- # if condition
- # filter_name do
- # yield self
- # end
- # else
- # yield self
- # end
- # end
- #
- name = "_conditional_callback_#{@kind}_#{next_id}"
- @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
- def #{name}(halted)
- if #{@compiled_options} && !halted
- #{@filter} do
- yield self
- end
- else
- yield self
- end
- end
- RUBY_EVAL
- "#{name}(halted) do"
- end
- end
-
- # This will supply contents for around and after filters, but not
- # before filters (for the backward pass).
- def end(key=nil, object=nil)
- return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")
-
- case @kind
when :after
- # after_save :filter_name, :if => :condition
<<-RUBY_EVAL
+ #{code}
if #{@compiled_options}
#{@filter}
end
RUBY_EVAL
when :around
+ name = define_conditional_callback
<<-RUBY_EVAL
+ #{name}(halted) do
+ #{code}
value
end
RUBY_EVAL
end
end
+
+ def one_time_conditions_valid?(object)
+ object.send("_one_time_conditions_valid_#{@callback_id}?")
+ end
+
private
+ # Compile around filters with conditions into proxy methods
+ # that contain the conditions.
+ #
+ # For `around_save :filter_name, :if => :condition':
+ #
+ # def _conditional_callback_save_17
+ # if condition
+ # filter_name do
+ # yield self
+ # end
+ # else
+ # yield self
+ # end
+ # end
+ #
+ def define_conditional_callback
+ name = "_conditional_callback_#{@kind}_#{next_id}"
+ @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
+ def #{name}(halted)
+ if #{@compiled_options} && !halted
+ #{@filter} do
+ yield self
+ end
+ else
+ yield self
+ end
+ end
+ RUBY_EVAL
+ name
+ end
+
# Options support the same options as filters themselves (and support
# symbols, string, procs, and objects), so compile a conditional
# expression based on the options
@@ -348,10 +341,20 @@ def compile(key=nil, object=nil)
method << "value = nil"
method << "halted = false"
- each do |callback|
- method << callback.start(key, object)
+ callbacks = yielding
+ applicable_callbacks_for(key, object).reverse_each do |callback|
+ callbacks = callback.apply(callbacks, key, object)
end
+ method << callbacks
+
+ method << "raise rescued_error if rescued_error" if config[:rescuable]
+ method << "halted ? false : (block_given? ? value : true)"
+ method.flatten.compact.join("\n")
+ end
+ # Returns part of method that evaluates the callback block
+ def yielding
+ method = []
if config[:rescuable]
method << "rescued_error = nil"
method << "begin"
@@ -364,14 +367,15 @@ def compile(key=nil, object=nil)
method << "rescued_error = e"
method << "end"
end
+ method.join("\n")
+ end
- reverse_each do |callback|
- method << callback.end(key, object)
+ # Selects callbacks that have valid <tt>:per_key</tt> condition
+ def applicable_callbacks_for(key, object)
+ return self unless key
+ select do |callback|
+ callback.one_time_conditions_valid?(object)
end
-
- method << "raise rescued_error if rescued_error" if config[:rescuable]
- method << "halted ? false : (block_given? ? value : true)"
- method.compact.join("\n")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.