diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-04-06 19:07:21 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-04-06 19:07:21 -0300 |
commit | 8b88df94ebda2e829782f514ff51caeaf5e694dd (patch) | |
tree | b1e0baa3ff26fd9285f6c1a751b5f97aa466b9b9 /activesupport/lib/active_support/callbacks.rb | |
parent | c539cc006179c98d418c6914ce567f657b6f1966 (diff) | |
parent | d2876141d08341ec67cf6a11a073d1acfb920de7 (diff) | |
download | rails-8b88df94ebda2e829782f514ff51caeaf5e694dd.tar.gz rails-8b88df94ebda2e829782f514ff51caeaf5e694dd.tar.bz2 rails-8b88df94ebda2e829782f514ff51caeaf5e694dd.zip |
Merge pull request #19029 from iainbeeston/skipping-undefined-callbacks
Raise ArgumentError if an unrecognised callback is skipped
Diffstat (limited to 'activesupport/lib/active_support/callbacks.rb')
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 53039dbf8b..814fd288cf 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -689,19 +689,27 @@ module ActiveSupport # class Writer < Person # skip_callback :validate, :before, :check_membership, if: -> { self.age > 18 } # end + # + # An <tt>ArgumentError</tt> will be raised if the callback has not + # already been set (unless the <tt>:raise</tt> option is set to <tt>false</tt>). def skip_callback(name, *filter_list, &block) type, filters, options = normalize_callback_params(filter_list, block) + options[:raise] = true unless options.key?(:raise) __update_callbacks(name) do |target, chain| filters.each do |filter| - filter = chain.find {|c| c.matches?(type, filter) } + callback = chain.find {|c| c.matches?(type, filter) } + + if !callback && options[:raise] + raise ArgumentError, "#{type.to_s.capitalize} #{name} callback #{filter.inspect} has not been defined" + end - if filter && options.any? - new_filter = filter.merge_conditional_options(chain, if_option: options[:if], unless_option: options[:unless]) - chain.insert(chain.index(filter), new_filter) + if callback && (options.key?(:if) || options.key?(:unless)) + new_callback = callback.merge_conditional_options(chain, if_option: options[:if], unless_option: options[:unless]) + chain.insert(chain.index(callback), new_callback) end - chain.delete(filter) + chain.delete(callback) end target.set_callbacks name, chain end |