Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8803: filter_in_wrong_place_fix_with_test.patch

File filter_in_wrong_place_fix_with_test.patch, 3.7 kB (added by kamal, 2 years ago)

seems related to #8341, use the same approach to remove left over before filters also

  • actionpack/test/controller/filters_test.rb

    old new  
    350350    end 
    351351  end 
    352352 
     353  class RenderingRescuedController < ActionController::Base 
     354    around_filter :catch_errors 
     355 
     356    #make sure the controller complains 
     357    def rescue_action(e); raise e; end 
     358 
     359  private 
     360    def catch_errors 
     361      begin 
     362        yield 
     363      rescue ErrorToRescue => ex 
     364        render :text => ex 
     365      end 
     366    end 
     367  end 
     368 
     369  class ExceptionInBeforeFilterController < RenderingRescuedController 
     370    before_filter :load_user 
     371     
     372    def show 
     373      render :text => "hello" 
     374    end 
     375 
     376  private 
     377    def load_user 
     378      raise ErrorToRescue.new("Load user failed.") 
     379    end 
     380  end 
     381 
     382  class ExceptionInActionController < RenderingRescuedController 
     383    before_filter :load_user 
     384 
     385    def show 
     386      raise ErrorToRescue.new("Show action failed.") 
     387    end 
     388 
     389  private 
     390    def load_user 
     391      @ran_filter ||= [] 
     392      @ran_filter << 'load_user' 
     393    end 
     394  end 
     395 
     396  class NonYieldingAroundFilterController < ActionController::Base 
     397    before_filter :filter_one 
     398    around_filter :yielding_filter 
     399    before_filter :filter_two 
     400 
     401    def index 
     402      render :inline => "index" 
     403    end 
     404 
     405    #make sure the controller complains 
     406    def rescue_action(e); raise e; end 
     407 
     408  private 
     409    def filter_one 
     410      @filters  ||= [] 
     411      @filters  << "filter_one" 
     412    end 
     413 
     414    def filter_two 
     415      @filters  ||= [] 
     416      @filters  << "filter_two" 
     417    end 
     418 
     419    def yielding_filter 
     420      @filters  ||= [] 
     421      @filters  << "zomg it didn't yield" 
     422    end 
     423  end 
     424 
    353425  def test_empty_filter_chain 
    354426    assert_equal 0, EmptyFilterChainController.filter_chain.size 
    355427    assert test_process(EmptyFilterChainController).template.assigns['action_executed'] 
     
    527599    assert_equal("I rescued this: #<FilterTest::ErrorToRescue: Something made the bad noise.>", response.body) 
    528600  end 
    529601 
     602  def test_a_rendering_rescued_around_filter_when_exception_in_before_filter 
     603    response = nil 
     604    assert_nothing_raised do 
     605      response = test_process(ExceptionInBeforeFilterController) 
     606    end 
     607    assert_equal "Load user failed.", response.body 
     608  end 
     609 
     610  def test_a_rendering_rescued_around_filter_when_exception_in_action 
     611    response = nil 
     612    assert_nothing_raised do 
     613      response = test_process(ExceptionInActionController) 
     614    end 
     615    assert_equal "Show action failed.", response.body 
     616  end 
     617 
     618  def test_non_yielding_around_filters 
     619    controller = NonYieldingAroundFilterController.new 
     620    assert_nothing_raised do 
     621      test_process(controller, "index") 
     622    end 
     623  end 
     624 
    530625  private 
    531626    def test_process(controller, action = "show") 
    532627      request = ActionController::TestRequest.new 
  • actionpack/lib/action_controller/filters.rb

    old new  
    714714        return index if aborted || nesting != 0 
    715715 
    716716        # if an around filter catches an exception during rendering and handles it, e.g. 
    717         # by rendering an error page, we might have left over around filters in the filter 
     717        # by rendering an error page, we might have left over around and before filters in the filter 
    718718        # chain. so skip over these. 
    719         index = index.next while (filter = chain[index]) && filter.type == :around 
     719        index = index.next while (filter = chain[index]) && [:around, :before].include?(filter.type) 
    720720 
    721721        # run after filters, if any 
    722722        while chain[index]