Permalink
Browse files

Make Filter#filter work with around filters

  • Loading branch information...
1 parent 9cda410 commit 10085114ce7ec6bc967fa701c49ef218f666efb5 @lifo lifo committed May 25, 2009
@@ -63,7 +63,7 @@ def caches_action(*actions)
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 @@ def initialize(options, &block)
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)
@@ -83,7 +83,7 @@ def cache_store=(store_option)
@@cache_store = ActiveSupport::Cache.lookup_store(store_option)
end
end
-
+
def initialize(*)
super
@template = _action_view
@@ -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 @@ def headers=(new_headers)
@_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
@@ -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 @@ def test_prepending_filter
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 @@ def test_prepending_and_appending_around_filter
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 @@ def test_dynamic_dispatch
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 @@ def test_conditional_skipping_of_filters
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 @@ def test_with_method
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 @@ def test_nested_filters
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 @@ def test_last_filter_in_multiple_before_filter_chain_halts
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
@@ -298,11 +298,23 @@ def #{method_name}
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

0 comments on commit 1008511

Please sign in to comment.