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

Changeset 3504

Show
Ignore:
Timestamp:
01/31/06 23:55:04 (3 years ago)
Author:
david
Message:

Added :only and :except controls to skip_before/after_filter just like for when you add filters [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r3497 r3504  
    11*SVN* 
     2 
     3* Added :only and :except controls to skip_before/after_filter just like for when you add filters [DHH] 
    24 
    35* Ensure that the instance variables are copied to the template when performing render :update. [Nicholas Seckar] 
  • trunk/actionpack/lib/action_controller/filters.rb

    r2873 r3504  
    252252      # filters, not procs. This is especially useful for managing the chain in inheritance hierarchies where only one out 
    253253      # of many sub-controllers need a different hierarchy. 
     254      # 
     255      # You can control the actions to skip the filter for with the <tt>:only</tt> and <tt>:except</tt> options,  
     256      # just like when you apply the filters. 
    254257      def skip_before_filter(*filters) 
    255         for filter in filters.flatten 
    256           write_inheritable_attribute("before_filters", read_inheritable_attribute("before_filters") - [ filter ]) 
     258        if conditions = extract_conditions!(filters) 
     259          conditions[:only], conditions[:except] = conditions[:except], conditions[:only] 
     260          add_action_conditions(filters, conditions) 
     261        else 
     262          for filter in filters.flatten 
     263            write_inheritable_attribute("before_filters", read_inheritable_attribute("before_filters") - [ filter ]) 
     264          end 
    257265        end 
    258266      end 
     
    261269      # filters, not procs. This is especially useful for managing the chain in inheritance hierarchies where only one out 
    262270      # of many sub-controllers need a different hierarchy. 
     271      # 
     272      # You can control the actions to skip the filter for with the <tt>:only</tt> and <tt>:except</tt> options,  
     273      # just like when you apply the filters. 
    263274      def skip_after_filter(*filters) 
    264         for filter in filters.flatten 
    265           write_inheritable_attribute("after_filters", read_inheritable_attribute("after_filters") - [ filter ]) 
     275        if conditions = extract_conditions!(filters) 
     276          conditions[:only], conditions[:except] = conditions[:except], conditions[:only] 
     277          add_action_conditions(filters, conditions) 
     278        else 
     279          for filter in filters.flatten 
     280            write_inheritable_attribute("after_filters", read_inheritable_attribute("after_filters") - [ filter ]) 
     281          end 
    266282        end 
    267283      end 
  • trunk/actionpack/test/controller/filters_test.rb

    r2873 r3504  
    44  class TestController < ActionController::Base 
    55    before_filter :ensure_login 
     6    after_filter  :clean_up 
    67 
    78    def show 
     
    1415        @ran_filter << "ensure_login" 
    1516      end 
     17       
     18      def clean_up 
     19        @ran_after_filter ||= [] 
     20        @ran_after_filter << "clean_up" 
     21      end 
    1622  end 
    1723 
     
    108114      end 
    109115  end 
     116 
     117  class ConditionalSkippingController < TestController 
     118    skip_before_filter :ensure_login, :only => [ :login ] 
     119    skip_after_filter  :clean_up,     :only => [ :login ] 
     120 
     121    def login 
     122      render :inline => "ran action" 
     123    end 
     124 
     125    def change_password 
     126      render :inline => "ran action" 
     127    end 
     128  end 
     129 
    110130 
    111131  class ProcController < PrependingController 
     
    344364  end 
    345365 
     366  def test_conditional_skipping_of_filters 
     367    assert_nil test_process(ConditionalSkippingController, "login").template.assigns["ran_filter"] 
     368    assert_equal %w( ensure_login ), test_process(ConditionalSkippingController, "change_password").template.assigns["ran_filter"] 
     369 
     370    assert_nil test_process(ConditionalSkippingController, "login").template.controller.instance_variable_get("@ran_after_filter") 
     371    assert_equal %w( clean_up ), test_process(ConditionalSkippingController, "change_password").template.controller.instance_variable_get("@ran_after_filter") 
     372  end 
     373 
    346374  private 
    347375    def test_process(controller, action = "show")