diff options
Diffstat (limited to 'activesupport/lib/active_support/callbacks.rb')
| -rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 75 |
1 files changed, 16 insertions, 59 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index c4cfc11372..37a184ed6d 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -76,7 +76,8 @@ module ActiveSupport # save # end def run_callbacks(kind, &block) - cbs = send("_#{kind}_callbacks") + cbs = self.class.send("_#{kind}_callbacks") + if cbs.empty? yield if block_given? else @@ -146,7 +147,7 @@ module ActiveSupport value = env.value halted = env.halted - if !halted + unless halted result = user_callback.call target, value env.halted = halted_lambda.call(target, result) if env.halted @@ -217,7 +218,7 @@ module ActiveSupport def self.halting(next_callback, user_callback) lambda { |env| env = next_callback.call env - if !env.halted + unless env.halted user_callback.call env.target, env.value end env @@ -284,7 +285,7 @@ module ActiveSupport target = env.target value = env.value - if !env.halted + unless env.halted user_callback.call(target, value) { env = next_callback.call env env.value @@ -330,7 +331,7 @@ module ActiveSupport new chain.name, filter, kind, options, chain.config end - attr_accessor :kind, :options, :name + attr_accessor :kind, :name attr_reader :chain_config def initialize(name, filter, kind, options, chain_config) @@ -338,39 +339,24 @@ module ActiveSupport @name = name @kind = kind @filter = filter - @options = options @key = compute_identifier filter - - deprecate_per_key_option(options) - normalize_options!(options) + @if = Array(options[:if]) + @unless = Array(options[:unless]) end def filter; @key; end def raw_filter; @filter; end - def deprecate_per_key_option(options) - if options[:per_key] - raise NotImplementedError, ":per_key option is no longer supported. Use generic :if and :unless options instead." - end - end - def merge(chain, new_options) - _options = { - :if => @options[:if].dup, - :unless => @options[:unless].dup + options = { + :if => @if.dup, + :unless => @unless.dup } - deprecate_per_key_option new_options - - _options[:if].concat Array(new_options.fetch(:unless, [])) - _options[:unless].concat Array(new_options.fetch(:if, [])) - - self.class.build chain, @filter, @kind, _options - end + options[:if].concat Array(new_options.fetch(:unless, [])) + options[:unless].concat Array(new_options.fetch(:if, [])) - def normalize_options!(options) - options[:if] = Array(options[:if]) - options[:unless] = Array(options[:unless]) + self.class.build chain, @filter, @kind, options end def matches?(_kind, _filter) @@ -463,37 +449,8 @@ module ActiveSupport end def conditions_lambdas - conditions = [] - - unless options[:if].empty? - lambdas = Array(options[:if]).map { |c| make_lambda c } - conditions.concat lambdas - end - - unless options[:unless].empty? - lambdas = Array(options[:unless]).map { |c| make_lambda c } - conditions.concat lambdas.map { |l| invert_lambda l } - end - conditions - end - - def _normalize_legacy_filter(kind, filter) - if !filter.respond_to?(kind) && filter.respond_to?(:filter) - message = "Filter object with #filter method is deprecated. Define method corresponding " \ - "to filter type (#before, #after or #around)." - ActiveSupport::Deprecation.warn message - filter.singleton_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def #{kind}(context, &block) filter(context, &block) end - RUBY_EVAL - elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around && !filter.respond_to?(:around) - message = "Filter object with #before and #after methods is deprecated. Define #around method instead." - ActiveSupport::Deprecation.warn message - def filter.around(context) - should_continue = before(context) - yield if should_continue - after(context) - end - end + @if.map { |c| make_lambda c } + + @unless.map { |c| invert_lambda make_lambda c } end end |
