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

Changeset 6669

Show
Ignore:
Timestamp:
05/06/07 04:10:26 (1 year ago)
Author:
nzkoz
Message:

Bug fix for the filter implementation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/filters.rb

    r6649 r6669  
    528528 
    529529        def find_filter_append_position(filters, filter_type) 
     530          # appending an after filter puts it at the end of the call chain 
     531          # before and around filters goe before the first after filter in the chain 
    530532          unless filter_type == :after 
    531533            filter_chain.each_with_index do |f,i| 
     
    537539 
    538540        def find_filter_prepend_position(filters, filter_type) 
     541          # prepending a before or around filter puts it at the front of the call chain 
     542          # after filters go before the first after filter in the chain 
    539543          if filter_type == :after 
    540544            filter_chain.each_with_index do |f,i| 
    541545              return i if f.after? 
    542546            end 
     547            return -1 
    543548          end 
    544549          return 0 
     
    685690      def call_filters(chain, index, nesting) 
    686691        # run before filters until we find an after filter or around filter 
    687         while true 
     692        while chain[index] 
    688693          filter, index = skip_excluded_filters(chain, index) 
    689           break unless filter 
     694          break unless filter # end of call chain reached 
    690695          case filter.type 
    691696          when :before 
     
    711716 
    712717        # run after filters, if any 
    713         while filter = chain[index] 
     718        while chain[index] 
    714719          filter, index = skip_excluded_filters(chain, index) 
     720          break unless filter 
    715721          case filter.type 
    716722          when :after 
  • trunk/actionpack/test/controller/filters_test.rb

    r6649 r6669  
    132132  end 
    133133 
     134  class EmptyFilterChainController < TestController 
     135    self.filter_chain.clear 
     136    def show 
     137      @action_executed = true 
     138      render :text => "yawp!" 
     139    end 
     140  end 
     141 
    134142  class PrependingController < TestController 
    135143    prepend_before_filter :wonderful_life 
     
    292300        self.action_name = params[:choose] 
    293301      end 
     302  end 
     303 
     304  class PrependingBeforeAndAfterController < ActionController::Base 
     305    prepend_before_filter :before_all 
     306    prepend_after_filter :after_all 
     307    before_filter :between_before_all_and_after_all 
     308 
     309    def before_all 
     310      @ran_filter ||= [] 
     311      @ran_filter << 'before_all' 
     312    end 
     313 
     314    def after_all 
     315      @ran_filter ||= [] 
     316      @ran_filter << 'after_all' 
     317    end 
     318 
     319    def between_before_all_and_after_all 
     320      @ran_filter ||= [] 
     321      @ran_filter << 'between_before_all_and_after_all' 
     322    end 
     323    def show 
     324      render :text => 'hello' 
     325    end 
     326  end 
     327 
     328  def test_empty_filter_chain 
     329    assert_equal 0, EmptyFilterChainController.filter_chain.size 
     330    assert test_process(EmptyFilterChainController).template.assigns['action_executed'] 
    294331  end 
    295332 
     
    425462      assert_equal action, response.body 
    426463    end 
     464  end 
     465 
     466  def test_running_prepended_before_and_after_filter 
     467    assert_equal 3, PrependingBeforeAndAfterController.filter_chain.length 
     468    response = test_process(PrependingBeforeAndAfterController) 
     469    assert_equal %w( before_all between_before_all_and_after_all after_all ), response.template.assigns["ran_filter"] 
    427470  end 
    428471