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

Ticket #2241: throw-to-abort-filters.patch

File throw-to-abort-filters.patch, 2.1 kB (added by fbeausoleil@ftml.net, 3 years ago)

Test, documentation and implementation to throw :abort in before filters

  • actionpack/test/controller/filters_test.rb

    old new  
    216216      end 
    217217  end 
    218218 
     219  class AbortingFilterController < ActionController::Base 
     220    before_filter :aborting_request 
     221 
     222    def show 
     223      render_text "ran action" 
     224    end 
     225 
     226    private 
     227      def aborting_request 
     228        throw :abort 
     229      end 
     230 
     231      def rescue_action(e) raise(e) end 
     232  end 
     233 
    219234  def test_added_filter_to_inheritance_graph 
    220235    assert_equal [ :fire_flash, :ensure_login ], TestController.before_filters 
    221236  end 
     
    342357    end 
    343358  end 
    344359 
     360  def test_prevent_filter_execution_by_throwing_abort 
     361    response = test_process(AbortingFilterController) 
     362    assert response.body.empty?, 'nothing should have been rendered' 
     363  end 
     364 
    345365  private 
    346366    def test_process(controller, action = "show") 
    347367      request = ActionController::TestRequest.new 
  • actionpack/lib/action_controller/filters.rb

    old new  
    297297      end 
    298298 
    299299      # Calls all the defined before-filter filters, which are added by using "before_filter :method". 
    300       # If any of the filters return false, no more filters will be executed and the action is aborted. 
     300      # A filter may cancel the request by doing one of the following: 
     301      # * +throw(:abort)+; 
     302      # * +return false+; 
     303      # * or performing the render itself. 
     304      # If any of these action is executed, the request will not call the regular action. 
    301305      def before_action #:doc: 
    302         call_filters(self.class.before_filters) 
     306        catch(:abort) do 
     307          return call_filters(self.class.before_filters) 
     308        end 
     309 
     310        return false 
    303311      end 
    304312 
    305313      # Calls all the defined after-filter filters, which are added by using "after_filter :method".