diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-01-31 23:55:04 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-01-31 23:55:04 +0000 |
commit | 2504982945828d24c9b799c408d71576508b4c15 (patch) | |
tree | 153a081b3ee884894798bc6fa6f9adcb1eee6110 /actionpack | |
parent | 2bf3fa076e822da1e55064ca7e9409af3fe52841 (diff) | |
download | rails-2504982945828d24c9b799c408d71576508b4c15.tar.gz rails-2504982945828d24c9b799c408d71576508b4c15.tar.bz2 rails-2504982945828d24c9b799c408d71576508b4c15.zip |
Added :only and :except controls to skip_before/after_filter just like for when you add filters [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3504 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/filters.rb | 24 | ||||
-rw-r--r-- | actionpack/test/controller/filters_test.rb | 28 |
3 files changed, 50 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 792480750c..f749a09f55 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added :only and :except controls to skip_before/after_filter just like for when you add filters [DHH] + * Ensure that the instance variables are copied to the template when performing render :update. [Nicholas Seckar] * Add the ability to call JavaScriptGenerator methods from helpers called in update blocks. [Sam Stephenson] Example: diff --git a/actionpack/lib/action_controller/filters.rb b/actionpack/lib/action_controller/filters.rb index 3f712629db..0149d52303 100644 --- a/actionpack/lib/action_controller/filters.rb +++ b/actionpack/lib/action_controller/filters.rb @@ -251,18 +251,34 @@ module ActionController #:nodoc: # Removes the specified filters from the +before+ filter chain. Note that this only works for skipping method-reference # filters, not procs. This is especially useful for managing the chain in inheritance hierarchies where only one out # of many sub-controllers need a different hierarchy. + # + # You can control the actions to skip the filter for with the <tt>:only</tt> and <tt>:except</tt> options, + # just like when you apply the filters. def skip_before_filter(*filters) - for filter in filters.flatten - write_inheritable_attribute("before_filters", read_inheritable_attribute("before_filters") - [ filter ]) + if conditions = extract_conditions!(filters) + conditions[:only], conditions[:except] = conditions[:except], conditions[:only] + add_action_conditions(filters, conditions) + else + for filter in filters.flatten + write_inheritable_attribute("before_filters", read_inheritable_attribute("before_filters") - [ filter ]) + end end end # Removes the specified filters from the +after+ filter chain. Note that this only works for skipping method-reference # filters, not procs. This is especially useful for managing the chain in inheritance hierarchies where only one out # of many sub-controllers need a different hierarchy. + # + # You can control the actions to skip the filter for with the <tt>:only</tt> and <tt>:except</tt> options, + # just like when you apply the filters. def skip_after_filter(*filters) - for filter in filters.flatten - write_inheritable_attribute("after_filters", read_inheritable_attribute("after_filters") - [ filter ]) + if conditions = extract_conditions!(filters) + conditions[:only], conditions[:except] = conditions[:except], conditions[:only] + add_action_conditions(filters, conditions) + else + for filter in filters.flatten + write_inheritable_attribute("after_filters", read_inheritable_attribute("after_filters") - [ filter ]) + end end end diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index da6dee1540..5065cef859 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../abstract_unit' class FilterTest < Test::Unit::TestCase class TestController < ActionController::Base before_filter :ensure_login + after_filter :clean_up def show render :inline => "ran action" @@ -13,6 +14,11 @@ class FilterTest < Test::Unit::TestCase @ran_filter ||= [] @ran_filter << "ensure_login" end + + def clean_up + @ran_after_filter ||= [] + @ran_after_filter << "clean_up" + end end class RenderingController < ActionController::Base @@ -108,6 +114,20 @@ class FilterTest < Test::Unit::TestCase end end + class ConditionalSkippingController < TestController + skip_before_filter :ensure_login, :only => [ :login ] + skip_after_filter :clean_up, :only => [ :login ] + + def login + render :inline => "ran action" + end + + def change_password + render :inline => "ran action" + end + end + + class ProcController < PrependingController before_filter(proc { |c| c.assigns["ran_proc_filter"] = true }) end @@ -343,6 +363,14 @@ class FilterTest < Test::Unit::TestCase end end + def test_conditional_skipping_of_filters + assert_nil test_process(ConditionalSkippingController, "login").template.assigns["ran_filter"] + assert_equal %w( ensure_login ), test_process(ConditionalSkippingController, "change_password").template.assigns["ran_filter"] + + assert_nil test_process(ConditionalSkippingController, "login").template.controller.instance_variable_get("@ran_after_filter") + assert_equal %w( clean_up ), test_process(ConditionalSkippingController, "change_password").template.controller.instance_variable_get("@ran_after_filter") + end + private def test_process(controller, action = "show") request = ActionController::TestRequest.new |