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

Changeset 5331

Show
Ignore:
Timestamp:
10/21/06 16:55:14 (2 years ago)
Author:
minam
Message:

More consistent implementation of filter replacement (thanks Martin! closes #5949)

Files:

Legend:

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

    r5301 r5331  
    249249      # will execute before the action on this controller is performed. 
    250250      def append_before_filter(*filters, &block) 
    251         new_filters, existing_filters = look_for_existing_filters(filters, :before) 
    252  
    253         append_filter_to_chain(new_filters, :before, &block) 
    254         skip_before_filter(existing_filters) unless existing_filters.empty? 
     251        append_filter_to_chain(filters, :before, &block) 
    255252      end 
    256253 
     
    267264      # that run _after_ actions on this controller are performed. 
    268265      def append_after_filter(*filters, &block) 
    269         new_filters, existing_filters = look_for_existing_filters(filters, :after) 
    270  
    271         prepend_filter_to_chain(new_filters, :after, &block) 
    272         skip_after_filter(existing_filters) unless existing_filters.empty? 
     266        prepend_filter_to_chain(filters, :after, &block) 
    273267      end 
    274268 
     
    412406        end 
    413407 
     408        def around? 
     409          true 
     410        end 
     411 
    414412        def call(controller, &block) 
    415413          raise(ActionControllerError, 'No filter type: Nothing to do here.') 
     
    423421          @filter.filter 
    424422        end 
     423 
     424        def around? 
     425          false 
     426        end 
    425427      end 
    426428 
     
    495497        def create_filters(filters, position, &block) #:nodoc: 
    496498          filters, conditions = extract_conditions(filters, &block) 
    497  
    498           filters.map! do |filter| 
    499             # change all the filters into instances of Filter derived classes 
    500             class_for_filter(filter).new(filter) 
    501           end 
    502  
    503           filters.map! do |filter| 
     499          filters.map! { |filter| find_or_create_filter(filter,position) } 
     500          update_conditions(filters, conditions) 
     501          filters 
     502        end 
     503 
     504        def find_or_create_filter(filter,position) 
     505          if found_filter = find_filter(filter) { |f| f.send("#{position}?") } 
     506            found_filter 
     507          else 
     508            f = class_for_filter(filter).new(filter) 
    504509            # apply proxy to filter if necessary 
    505510            case position 
    506511            when :before 
    507               BeforeFilterProxy.new(filter
     512              BeforeFilterProxy.new(f
    508513            when :after 
    509               AfterFilterProxy.new(filter
     514              AfterFilterProxy.new(f
    510515            else 
    511               filter 
     516              f 
    512517            end 
    513518          end 
    514  
    515           update_conditions(filters, conditions) 
    516  
    517           filters 
    518519        end 
    519520 
     
    604605          end 
    605606        end 
    606  
    607         def look_for_existing_filters(filters, which) 
    608           filters, options = extract_conditions(filters) 
    609           old_filters = [] 
    610  
    611           filter_chain.select(&"#{which}?".to_sym).each do |f| 
    612             old_filters << f.filter if filters.include?(f.filter) 
    613           end 
    614            
    615           new_filters = filters - old_filters + [options] 
    616  
    617           if options[:except] 
    618             old_filters << { :only => options[:except] } 
    619           elsif options[:only] 
    620             old_filters << { :except => options[:only] } 
    621           else 
    622             old_filters = [] 
    623           end 
    624  
    625           [new_filters, old_filters] 
    626         end 
    627607    end 
    628608