From acbf6d1cdd7f29aadcebc46940e3f2f28b6a22a6 Mon Sep 17 00:00:00 2001 From: Ben VandenBos Date: Fri, 29 Aug 2008 10:48:57 -0700 Subject: [PATCH] Adding Regexp support for :only and :except options on filter_chains --- actionpack/lib/action_controller/filters.rb | 16 +++++++-- actionpack/test/controller/filters_test.rb | 49 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/filters.rb b/actionpack/lib/action_controller/filters.rb index 1d7236f..fd0e4d9 100644 --- a/actionpack/lib/action_controller/filters.rb +++ b/actionpack/lib/action_controller/filters.rb @@ -145,9 +145,17 @@ module ActionController #:nodoc: def included_in_action?(controller, options) if options[:only] - options[:only].include?(controller.action_name) + if options[:only].is_a?(Regexp) + options[:only].match(controller.action_name) + else + options[:only].include?(controller.action_name) + end elsif options[:except] - !options[:except].include?(controller.action_name) + if options[:except].is_a?(Regexp) + !options[:except].match(controller.action_name) + else + !options[:except].include?(controller.action_name) + end else true end @@ -160,7 +168,9 @@ module ActionController #:nodoc: def convert_only_and_except_options_to_sets_of_strings(opts) [:only, :except].each do |key| if values = opts[key] - opts[key] = Array(values).map(&:to_s).to_set + if !values.is_a?(Regexp) + opts[key] = Array(values).map(&:to_s).to_set + end end end end diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 3652c48..bae6e37 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -417,6 +417,55 @@ class FilterTest < Test::Unit::TestCase end end + + class RegexpFilterController < ActionController::Base + before_filter :filter_one, :only => Regexp.new(/.*_create/) + around_filter :filter_two, :only => Regexp.new(/.*_update/) + before_filter :filter_three, :except => Regexp.new(/.*_destroy/) + + def filter_one + @filters ||= [] + @filters << 'filter_one' + end + + def filter_two + @filters ||= [] + @filters << 'filter_two' + end + + def filter_three + @filters ||= [] + @filters << 'filter_three' + end + + end + + def test_before_filter_regexp_matching_regexp + controller = RegexpFilterController.new + assert_nothing_raised do + test_process(controller, "widget_create") + end + filters = controller.instance_variable_get("@filters") + assert_equal(['filter_one', 'filter_three'], filters) + end + + def test_before_filter_regexp_not_matching_regexp + controller = RegexpFilterController.new + assert_nothing_raised do + test_process(controller, "widget_destroy") + end + filters = controller.instance_variable_get("@filters") + assert_equal(nil, filters) + end + + def test_around_filter_regexp_matching_regexp + controller = RegexpFilterController.new + assert_nothing_raised do + test_process(controller, "widget_update") + end + filters = controller.instance_variable_get("@filters") + assert_equal(['filter_two'], filters) + end def test_non_yielding_around_filters_not_returning_false_do_not_raise controller = NonYieldingAroundFilterController.new -- 1.5.6.3