diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-05-25 18:05:58 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-05-25 18:09:12 +0200 |
commit | 10085114ce7ec6bc967fa701c49ef218f666efb5 (patch) | |
tree | 865841b3f014024f5f8b8cc44462aca699b6eff1 | |
parent | 9cda410d818353a5c27cbd5df754f1d1e290927d (diff) | |
download | rails-10085114ce7ec6bc967fa701c49ef218f666efb5.tar.gz rails-10085114ce7ec6bc967fa701c49ef218f666efb5.tar.bz2 rails-10085114ce7ec6bc967fa701c49ef218f666efb5.zip |
Make Filter#filter work with around filters
-rw-r--r-- | actionpack/lib/action_controller/caching/actions.rb | 24 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/compatibility.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/testing.rb | 8 | ||||
-rw-r--r-- | actionpack/test/controller/filters_test.rb | 113 | ||||
-rw-r--r-- | activesupport/lib/active_support/new_callbacks.rb | 22 |
5 files changed, 93 insertions, 76 deletions
diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index 430db3932f..3646ff1af9 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -63,7 +63,7 @@ module ActionController #:nodoc: cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options) # TODO: Remove this once new base is swapped in. - if defined?(Http) + if defined?(ActionController::Http) around_filter cache_filter, filter_options else around_filter(filter_options) do |controller, action| @@ -92,16 +92,18 @@ module ActionController #:nodoc: end # TODO: Remove once New Base is merged - def filter(controller, action) - should_continue = before(controller) - action.call if should_continue - after(controller) - end - - def around_process_action(controller) - should_continue = before(controller) - yield if should_continue - after(controller) + if defined?(ActionController::Http) + def around_process_action(controller) + should_continue = before(controller) + yield if should_continue + after(controller) + end + else + def filter(controller, action) + should_continue = before(controller) + action.call if should_continue + after(controller) + end end def before(controller) diff --git a/actionpack/lib/action_controller/new_base/compatibility.rb b/actionpack/lib/action_controller/new_base/compatibility.rb index b3190486e8..4245ba982b 100644 --- a/actionpack/lib/action_controller/new_base/compatibility.rb +++ b/actionpack/lib/action_controller/new_base/compatibility.rb @@ -83,7 +83,7 @@ module ActionController @@cache_store = ActiveSupport::Cache.lookup_store(store_option) end end - + def initialize(*) super @template = _action_view diff --git a/actionpack/lib/action_controller/new_base/testing.rb b/actionpack/lib/action_controller/new_base/testing.rb index d8c3421587..78051a6252 100644 --- a/actionpack/lib/action_controller/new_base/testing.rb +++ b/actionpack/lib/action_controller/new_base/testing.rb @@ -1,5 +1,6 @@ module ActionController module Testing + extend ActiveSupport::DependencyModule # OMG MEGA HAX def process_with_new_base_test(request, response) @@ -26,5 +27,12 @@ module ActionController @_response ||= ActionDispatch::Response.new @_response.headers.replace(new_headers) end + + module ClassMethods + def before_filters + _process_action_callbacks.find_all{|x| x.kind == :before}.map{|x| x.name} + end + end + end end diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 22aa50adb2..bdcc24b371 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -8,8 +8,7 @@ class << ActionController::Base end end -class FilterTest < Test::Unit::TestCase - include ActionController::TestProcess +class FilterTest < ActionController::TestCase class TestController < ActionController::Base before_filter :ensure_login @@ -468,108 +467,108 @@ class FilterTest < Test::Unit::TestCase def test_running_filters test_process(PrependingController) - assert_equal %w( wonderful_life ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( wonderful_life ensure_login ), assigns["ran_filter"] end def test_running_filters_with_proc test_process(ProcController) - assert @controller.template.assigns["ran_proc_filter"] + assert assigns["ran_proc_filter"] end def test_running_filters_with_implicit_proc test_process(ImplicitProcController) - assert @controller.template.assigns["ran_proc_filter"] + assert assigns["ran_proc_filter"] end def test_running_filters_with_class test_process(AuditController) - assert @controller.template.assigns["was_audited"] + assert assigns["was_audited"] end def test_running_anomolous_yet_valid_condition_filters test_process(AnomolousYetValidConditionController) - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] - assert @controller.template.assigns["ran_class_filter"] - assert @controller.template.assigns["ran_proc_filter1"] - assert @controller.template.assigns["ran_proc_filter2"] + assert_equal %w( ensure_login ), assigns["ran_filter"] + assert assigns["ran_class_filter"] + assert assigns["ran_proc_filter1"] + assert assigns["ran_proc_filter2"] test_process(AnomolousYetValidConditionController, "show_without_filter") - assert_equal nil, @controller.template.assigns["ran_filter"] - assert !@controller.template.assigns["ran_class_filter"] - assert !@controller.template.assigns["ran_proc_filter1"] - assert !@controller.template.assigns["ran_proc_filter2"] + assert_equal nil, assigns["ran_filter"] + assert !assigns["ran_class_filter"] + assert !assigns["ran_proc_filter1"] + assert !assigns["ran_proc_filter2"] end def test_running_conditional_options test_process(ConditionalOptionsFilter) - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login ), assigns["ran_filter"] end def test_running_collection_condition_filters test_process(ConditionalCollectionFilterController) - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login ), assigns["ran_filter"] test_process(ConditionalCollectionFilterController, "show_without_filter") - assert_equal nil, @controller.template.assigns["ran_filter"] + assert_equal nil, assigns["ran_filter"] test_process(ConditionalCollectionFilterController, "another_action") - assert_equal nil, @controller.template.assigns["ran_filter"] + assert_equal nil, assigns["ran_filter"] end def test_running_only_condition_filters test_process(OnlyConditionSymController) - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login ), assigns["ran_filter"] test_process(OnlyConditionSymController, "show_without_filter") - assert_equal nil, @controller.template.assigns["ran_filter"] + assert_equal nil, assigns["ran_filter"] test_process(OnlyConditionProcController) - assert @controller.template.assigns["ran_proc_filter"] + assert assigns["ran_proc_filter"] test_process(OnlyConditionProcController, "show_without_filter") - assert !@controller.template.assigns["ran_proc_filter"] + assert !assigns["ran_proc_filter"] test_process(OnlyConditionClassController) - assert @controller.template.assigns["ran_class_filter"] + assert assigns["ran_class_filter"] test_process(OnlyConditionClassController, "show_without_filter") - assert !@controller.template.assigns["ran_class_filter"] + assert !assigns["ran_class_filter"] end def test_running_except_condition_filters test_process(ExceptConditionSymController) - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login ), assigns["ran_filter"] test_process(ExceptConditionSymController, "show_without_filter") - assert_equal nil, @controller.template.assigns["ran_filter"] + assert_equal nil, assigns["ran_filter"] test_process(ExceptConditionProcController) - assert @controller.template.assigns["ran_proc_filter"] + assert assigns["ran_proc_filter"] test_process(ExceptConditionProcController, "show_without_filter") - assert !@controller.template.assigns["ran_proc_filter"] + assert !assigns["ran_proc_filter"] test_process(ExceptConditionClassController) - assert @controller.template.assigns["ran_class_filter"] + assert assigns["ran_class_filter"] test_process(ExceptConditionClassController, "show_without_filter") - assert !@controller.template.assigns["ran_class_filter"] + assert !assigns["ran_class_filter"] end def test_running_before_and_after_condition_filters test_process(BeforeAndAfterConditionController) - assert_equal %w( ensure_login clean_up_tmp), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login clean_up_tmp), assigns["ran_filter"] test_process(BeforeAndAfterConditionController, "show_without_filter") - assert_equal nil, @controller.template.assigns["ran_filter"] + assert_equal nil, assigns["ran_filter"] end def test_around_filter test_process(AroundFilterController) - assert @controller.template.assigns["before_ran"] - assert @controller.template.assigns["after_ran"] + assert assigns["before_ran"] + assert assigns["after_ran"] end def test_before_after_class_filter test_process(BeforeAfterClassFilterController) - assert @controller.template.assigns["before_ran"] - assert @controller.template.assigns["after_ran"] + assert assigns["before_ran"] + assert assigns["after_ran"] end def test_having_properties_in_around_filter test_process(AroundFilterController) - assert_equal "before and after", @controller.template.assigns["execution_log"] + assert_equal "before and after", assigns["execution_log"] end def test_prepending_and_appending_around_filter @@ -582,7 +581,7 @@ class FilterTest < Test::Unit::TestCase def test_rendering_breaks_filtering_chain response = test_process(RenderingController) assert_equal "something else", response.body - assert !@controller.template.assigns["ran_action"] + assert !assigns["ran_action"] end def test_filters_with_mixed_specialization_run_in_order @@ -608,26 +607,26 @@ class FilterTest < Test::Unit::TestCase def test_running_prepended_before_and_after_filter test_process(PrependingBeforeAndAfterController) - assert_equal %w( before_all between_before_all_and_after_all after_all ), @controller.template.assigns["ran_filter"] + assert_equal %w( before_all between_before_all_and_after_all after_all ), assigns["ran_filter"] end def test_skipping_and_limiting_controller test_process(SkippingAndLimitedController, "index") - assert_equal %w( ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login ), assigns["ran_filter"] test_process(SkippingAndLimitedController, "public") - assert_nil @controller.template.assigns["ran_filter"] + assert_nil assigns["ran_filter"] end def test_skipping_and_reordering_controller test_process(SkippingAndReorderingController, "index") - assert_equal %w( find_record ensure_login ), @controller.template.assigns["ran_filter"] + assert_equal %w( find_record ensure_login ), assigns["ran_filter"] end def test_conditional_skipping_of_filters test_process(ConditionalSkippingController, "login") - assert_nil @controller.template.assigns["ran_filter"] + assert_nil assigns["ran_filter"] test_process(ConditionalSkippingController, "change_password") - assert_equal %w( ensure_login find_user ), @controller.template.assigns["ran_filter"] + assert_equal %w( ensure_login find_user ), assigns["ran_filter"] test_process(ConditionalSkippingController, "login") assert_nil @controller.template.controller.instance_variable_get("@ran_after_filter") @@ -637,23 +636,23 @@ class FilterTest < Test::Unit::TestCase def test_conditional_skipping_of_filters_when_parent_filter_is_also_conditional test_process(ChildOfConditionalParentController) - assert_equal %w( conditional_in_parent conditional_in_parent ), @controller.template.assigns['ran_filter'] + assert_equal %w( conditional_in_parent conditional_in_parent ), assigns['ran_filter'] test_process(ChildOfConditionalParentController, 'another_action') - assert_nil @controller.template.assigns['ran_filter'] + assert_nil assigns['ran_filter'] end def test_condition_skipping_of_filters_when_siblings_also_have_conditions test_process(ChildOfConditionalParentController) - assert_equal %w( conditional_in_parent conditional_in_parent ), @controller.template.assigns['ran_filter'], "1" + assert_equal %w( conditional_in_parent conditional_in_parent ), assigns['ran_filter'], "1" test_process(AnotherChildOfConditionalParentController) - assert_equal nil, @controller.template.assigns['ran_filter'] + assert_equal nil, assigns['ran_filter'] test_process(ChildOfConditionalParentController) - assert_equal %w( conditional_in_parent conditional_in_parent ), @controller.template.assigns['ran_filter'] + assert_equal %w( conditional_in_parent conditional_in_parent ), assigns['ran_filter'] end def test_changing_the_requirements test_process(ChangingTheRequirementsController, "go_wild") - assert_equal nil, @controller.template.assigns['ran_filter'] + assert_equal nil, assigns['ran_filter'] end def test_a_rescuing_around_filter @@ -806,9 +805,8 @@ class ControllerWithTwoLessFilters < ControllerWithAllTypesOfFilters skip_filter :after end -class YieldingAroundFiltersTest < Test::Unit::TestCase +class YieldingAroundFiltersTest < ActionController::TestCase include PostsController::AroundExceptions - include ActionController::TestProcess def test_base controller = PostsController @@ -846,8 +844,8 @@ class YieldingAroundFiltersTest < Test::Unit::TestCase def test_with_proc test_process(ControllerWithProcFilter,'no_raise') - assert @controller.template.assigns['before'] - assert @controller.template.assigns['after'] + assert assigns['before'] + assert assigns['after'] end def test_nested_filters @@ -868,12 +866,12 @@ class YieldingAroundFiltersTest < Test::Unit::TestCase def test_filter_order_with_all_filter_types test_process(ControllerWithAllTypesOfFilters,'no_raise') - assert_equal 'before around (before yield) around_again (before yield) around_again (after yield) around (after yield) after', @controller.template.assigns['ran_filter'].join(' ') + assert_equal 'before around (before yield) around_again (before yield) around_again (after yield) around (after yield) after', assigns['ran_filter'].join(' ') end def test_filter_order_with_skip_filter_method test_process(ControllerWithTwoLessFilters,'no_raise') - assert_equal 'before around (before yield) around (after yield)', @controller.template.assigns['ran_filter'].join(' ') + assert_equal 'before around (before yield) around (after yield)', assigns['ran_filter'].join(' ') end def test_first_filter_in_multiple_before_filter_chain_halts @@ -903,9 +901,6 @@ class YieldingAroundFiltersTest < Test::Unit::TestCase protected def test_process(controller, action = "show") @controller = controller.is_a?(Class) ? controller.new : controller - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - process(action) end end diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index f8108780f1..039d0fa658 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -298,11 +298,23 @@ module ActiveSupport kind, name = @kind, @name @klass.send(:define_method, "#{method_name}_object") { filter } - @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def #{method_name}(&blk) - #{method_name}_object.send("#{kind}_#{name}", self, &blk) - end - RUBY_EVAL + if kind == :around + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + if :#{kind} == :around && #{method_name}_object.respond_to?(:filter) + #{method_name}_object.send("filter", self, &blk) + else + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end + end + RUBY_EVAL + else + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end + RUBY_EVAL + end method_name end end |