From 971e2438d98326c994ec6d3ef8e37b7e868ed6e2 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 2 Jun 2009 19:00:59 -0700 Subject: Simplify callbacks to use less metaprogramming --- .../lib/action_controller/abstract/callbacks.rb | 30 +++++++++++----------- .../test/abstract_controller/callbacks_test.rb | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/abstract/callbacks.rb b/actionpack/lib/action_controller/abstract/callbacks.rb index affe053bac..dd4213e847 100644 --- a/actionpack/lib/action_controller/abstract/callbacks.rb +++ b/actionpack/lib/action_controller/abstract/callbacks.rb @@ -32,32 +32,32 @@ module AbstractController skip_around_filter(*names, &blk) end + def _insert_callbacks(names, block) + options = names.last.is_a?(Hash) ? names.pop : {} + _normalize_callback_options(options) + names.push(block) if block + names.each do |name| + yield name, options + end + end + [:before, :after, :around].each do |filter| class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{filter}_filter(*names, &blk) - options = names.last.is_a?(Hash) ? names.pop : {} - _normalize_callback_options(options) - names.push(blk) if block_given? - names.each do |name| - process_action_callback(:#{filter}, name, options) + _insert_callbacks(names, blk) do |name, options| + _set_callback(:process_action, :#{filter}, name, options) end end def prepend_#{filter}_filter(*names, &blk) - options = names.last.is_a?(Hash) ? names.pop : {} - _normalize_callback_options(options) - names.push(blk) if block_given? - names.each do |name| - process_action_callback(:#{filter}, name, options.merge(:prepend => true)) + _insert_callbacks(names, blk) do |name, options| + _set_callback(:process_action, :#{filter}, name, options.merge(:prepend => true)) end end def skip_#{filter}_filter(*names, &blk) - options = names.last.is_a?(Hash) ? names.pop : {} - _normalize_callback_options(options) - names.push(blk) if block_given? - names.each do |name| - skip_process_action_callback(:#{filter}, name, options) + _insert_callbacks(names, blk) do |name, options| + _skip_callback(:process_action, :#{filter}, name, options) end end diff --git a/actionpack/test/abstract_controller/callbacks_test.rb b/actionpack/test/abstract_controller/callbacks_test.rb index 1de60868c3..826e76892e 100644 --- a/actionpack/test/abstract_controller/callbacks_test.rb +++ b/actionpack/test/abstract_controller/callbacks_test.rb @@ -8,7 +8,7 @@ module AbstractController end class Callback1 < ControllerWithCallbacks - process_action_callback :before, :first + _set_callback :process_action, :before, :first def first @text = "Hello world" -- cgit v1.2.3