1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

AS::Callbacks: :skip_after_callbacks_if_terminated option

This commit is contained in:
Bogdan Gusiev 2012-02-03 17:57:53 +02:00
parent 2abaa19e77
commit 7661955634
5 changed files with 12 additions and 7 deletions

View file

@ -8,7 +8,7 @@ module AbstractController
include ActiveSupport::Callbacks include ActiveSupport::Callbacks
included do included do
define_callbacks :process_action, :terminator => "response_body" define_callbacks :process_action, :terminator => "response_body", :skip_after_callbacks_if_terminated => true
end end
# Override AbstractController::Base's process_action to run the # Override AbstractController::Base's process_action to run the
@ -167,7 +167,6 @@ module AbstractController
# for details on the allowed parameters. # for details on the allowed parameters.
def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk) def #{filter}_filter(*names, &blk) # def before_filter(*names, &blk)
_insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options| _insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options|
options[:if] = (Array(options[:if]) << "!halted") if #{filter == :after} # options[:if] = (Array(options[:if]) << "!halted") if false
set_callback(:process_action, :#{filter}, name, options) # set_callback(:process_action, :before, name, options) set_callback(:process_action, :#{filter}, name, options) # set_callback(:process_action, :before, name, options)
end # end end # end
end # end end # end
@ -176,7 +175,6 @@ module AbstractController
# for details on the allowed parameters. # for details on the allowed parameters.
def prepend_#{filter}_filter(*names, &blk) # def prepend_before_filter(*names, &blk) def prepend_#{filter}_filter(*names, &blk) # def prepend_before_filter(*names, &blk)
_insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options| _insert_callbacks(names, blk) do |name, options| # _insert_callbacks(names, blk) do |name, options|
options[:if] = (Array(options[:if]) << "!halted") if #{filter == :after} # options[:if] = (Array(options[:if]) << "!halted") if false
set_callback(:process_action, :#{filter}, name, options.merge(:prepend => true)) # set_callback(:process_action, :before, name, options.merge(:prepend => true)) set_callback(:process_action, :#{filter}, name, options.merge(:prepend => true)) # set_callback(:process_action, :before, name, options.merge(:prepend => true))
end # end end # end
end # end end # end

View file

@ -88,6 +88,7 @@ module ActiveModel
options = callbacks.extract_options! options = callbacks.extract_options!
options = { options = {
:terminator => "result == false", :terminator => "result == false",
:skip_after_callbacks_if_terminated => true,
:scope => [:kind, :name], :scope => [:kind, :name],
:only => [:before, :around, :after] :only => [:before, :around, :after]
}.merge(options) }.merge(options)
@ -124,7 +125,7 @@ module ActiveModel
def self.after_#{callback}(*args, &block) def self.after_#{callback}(*args, &block)
options = args.extract_options! options = args.extract_options!
options[:prepend] = true options[:prepend] = true
options[:if] = Array(options[:if]) << "!halted && value != false" options[:if] = Array(options[:if]) << "value != false"
set_callback(:#{callback}, :after, *(args << options), &block) set_callback(:#{callback}, :after, *(args << options), &block)
end end
CALLBACK CALLBACK

View file

@ -23,7 +23,7 @@ module ActiveModel
included do included do
include ActiveSupport::Callbacks include ActiveSupport::Callbacks
define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name] define_callbacks :validation, :terminator => "result == false", :skip_after_callbacks_if_terminated => true, :scope => [:kind, :name]
end end
module ClassMethods module ClassMethods
@ -40,7 +40,6 @@ module ActiveModel
options = args.extract_options! options = args.extract_options!
options[:prepend] = true options[:prepend] = true
options[:if] = Array(options[:if]) options[:if] = Array(options[:if])
options[:if] << "!halted"
options[:if].unshift("self.validation_context == :#{options[:on]}") if options[:on] options[:if].unshift("self.validation_context == :#{options[:on]}") if options[:on]
set_callback(:validation, :after, *(args << options), &block) set_callback(:validation, :after, *(args << options), &block)
end end

View file

@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* `AS::Callbacks#define_callbacks`: add `:skip_after_callbacks_if_terminated` option.
* Add html_escape_once to ERB::Util, and delegate escape_once tag helper to it. *Carlos Antonio da Silva* * Add html_escape_once to ERB::Util, and delegate escape_once tag helper to it. *Carlos Antonio da Silva*
* Remove ActiveSupport::TestCase#pending method, use `skip` instead. *Carlos Antonio da Silva* * Remove ActiveSupport::TestCase#pending method, use `skip` instead. *Carlos Antonio da Silva*

View file

@ -169,7 +169,7 @@ module ActiveSupport
when :after when :after
<<-RUBY_EVAL <<-RUBY_EVAL
#{code} #{code}
if #{@compiled_options} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options}
#{@filter} #{@filter}
end end
RUBY_EVAL RUBY_EVAL
@ -528,6 +528,11 @@ module ActiveSupport
# other callbacks are not executed. Defaults to "false", meaning no value # other callbacks are not executed. Defaults to "false", meaning no value
# halts the chain. # halts the chain.
# #
# * <tt>:skip_after_callbacks_if_terminated</tt> - Determines if after callbacks should be terminated
# by the <tt>:terminator</tt> option. By default after callbacks executed no matter
# if callback chain was terminated or not.
# Option makes sence only when <tt>:terminator</tt> option is specified.
#
# * <tt>:rescuable</tt> - By default, after filters are not executed if # * <tt>:rescuable</tt> - By default, after filters are not executed if
# the given block or a before filter raises an error. By setting this option # the given block or a before filter raises an error. By setting this option
# to <tt>true</tt> exception raised by given block is stored and after # to <tt>true</tt> exception raised by given block is stored and after