Changeset 7984
- Timestamp:
- 10/21/07 18:58:17 (11 months ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/filters.rb (modified) (16 diffs)
- trunk/actionpack/test/controller/filters_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r7978 r7984 1 1 *SVN* 2 3 * Changed before_filter halting to happen automatically on render or redirect but no longer on simply returning false [DHH] 2 4 3 5 * Ensure that cookies handle array values correctly. Closes #9937 [queso] trunk/actionpack/lib/action_controller/filters.rb
r7719 r7984 8 8 end 9 9 10 # Filters enable controllers to run shared pre and postprocessing code for its actions. These filters can be used to do10 # Filters enable controllers to run shared pre- and post-processing code for its actions. These filters can be used to do 11 11 # authentication, caching, or auditing before the intended action is performed. Or to do localization or output 12 12 # compression after the action has been performed. Filters have access to the request, response, and all the instance … … 37 37 # 38 38 # Now any actions performed on the BankController will have the audit method called before. On the VaultController, 39 # first the audit method is called, then the verify_credentials method. If the audit method re turns false, then39 # first the audit method is called, then the verify_credentials method. If the audit method renders or redirects, then 40 40 # verify_credentials and the intended action are never called. 41 41 # … … 66 66 # 67 67 # class WeblogController < ActionController::Base 68 # before_filter { |controller| falseif controller.params["stop_action"] }68 # before_filter { |controller| head(400) if controller.params["stop_action"] } 69 69 # end 70 70 # … … 91 91 # 92 92 # The filter chain for the CheckoutController is now <tt>:ensure_items_in_cart, :ensure_items_in_stock,</tt> 93 # <tt>:verify_open_shop</tt>. So if either of the ensure filters re turn false, we'll never get around to see if the shop93 # <tt>:verify_open_shop</tt>. So if either of the ensure filters renders or redirects, we'll never get around to see if the shop 94 94 # is open or not. 95 95 # … … 143 143 # 144 144 # class Authorizer 145 # # This will run before the action. Re turning falseaborts the action.145 # # This will run before the action. Redirecting aborts the action. 146 146 # def before(controller) 147 # if user.authorized? 148 # return true 149 # else 150 # redirect_to login_url 151 # return false 147 # unless user.authorized? 148 # redirect_to(login_url) 152 149 # end 153 150 # end 154 151 # 155 # # This will run after the action if and only if before returned true.152 # # This will run after the action if and only if before did not render or redirect. 156 153 # def after(controller) 157 154 # end … … 159 156 # 160 157 # If the filter has before and after methods, the before method will be 161 # called before the action. If before re turns false, the filter chain is158 # called before the action. If before renders or redirects, the filter chain is 162 159 # halted and after will not be run. See Filter Chain Halting below for 163 160 # an example. … … 219 216 # before a controller action is run. This is useful, for example, to deny 220 217 # access to unauthenticated users or to redirect from http to https. 221 # Simply return false from the filter or call render or redirect.222 # After filters will not be executed if the filterchain is halted.218 # Simply call render or redirect. After filters will not be executed if the filter 219 # chain is halted. 223 220 # 224 221 # Around filters halt the request unless the action block is called. … … 245 242 # 246 243 # If #around returns before yielding, #after will still not be run. The #before 247 # filter and controller action will not be run. If #before re turns false,244 # filter and controller action will not be run. If #before renders or redirects, 248 245 # the second half of #around and will still run but #after and the 249 246 # action will not. If #around fails to yield, #after will not be run. … … 442 439 def run(controller) 443 440 # only filters returning false are halted. 444 if false == @filter.call(controller) 445 controller.send! :halt_filter_chain, @filter, :returned_false 441 @filter.call(controller) 442 if controller.send!(:performed?) 443 controller.send!(:halt_filter_chain, @filter, :rendered_or_redirected) 446 444 end 447 445 end … … 658 656 return filter unless filter_responds_to_before_and_after(filter) 659 657 Proc.new do |controller, action| 660 if filter.before(controller) == false 661 controller.send! :halt_filter_chain, filter, :returned_false 658 filter.before(controller) 659 660 if controller.send!(:performed?) 661 controller.send!(:halt_filter_chain, filter, :rendered_or_redirected) 662 662 else 663 663 begin … … 711 711 filter, index = skip_excluded_filters(chain, index) 712 712 break unless filter # end of call chain reached 713 713 714 case filter.type 714 715 when :before … … 718 719 when :around 719 720 yielded = false 721 720 722 filter.call(self) do 721 723 yielded = true … … 723 725 index = call_filters(chain, index.next, nesting.next) 724 726 end 727 725 728 halt_filter_chain(filter, :did_not_yield) unless yielded 729 726 730 break 727 731 else … … 729 733 end 730 734 end 735 731 736 index 732 737 end … … 734 739 def run_after_filters(chain, index) 735 740 seen_after_filter = false 741 736 742 while chain[index] 737 743 filter, index = skip_excluded_filters(chain, index) 738 744 break unless filter # end of call chain reached 745 739 746 case filter.type 740 747 when :after … … 745 752 raise ActionControllerError, "filter #{filter.inspect} was in the wrong place!" if seen_after_filter 746 753 end 754 747 755 index = index.next 748 756 end 757 749 758 index.next 750 759 end trunk/actionpack/test/controller/filters_test.rb
r7719 r7984 45 45 define_method "try_#{i}" do 46 46 instance_variable_set :@try, i 47 action_name != "fail_#{i}" 47 if action_name == "fail_#{i}" 48 head(404) 49 end 48 50 end 49 51 end … … 824 826 controller = ::FilterTest::TestMultipleFiltersController.new 825 827 response = test_process(controller, 'fail_1') 826 assert_equal ' ', response.body828 assert_equal ' ', response.body 827 829 assert_equal 1, controller.instance_variable_get(:@try) 828 830 assert controller.instance_variable_get(:@before_filter_chain_aborted) … … 832 834 controller = ::FilterTest::TestMultipleFiltersController.new 833 835 response = test_process(controller, 'fail_2') 834 assert_equal ' ', response.body836 assert_equal ' ', response.body 835 837 assert_equal 2, controller.instance_variable_get(:@try) 836 838 assert controller.instance_variable_get(:@before_filter_chain_aborted) … … 840 842 controller = ::FilterTest::TestMultipleFiltersController.new 841 843 response = test_process(controller, 'fail_3') 842 assert_equal ' ', response.body844 assert_equal ' ', response.body 843 845 assert_equal 3, controller.instance_variable_get(:@try) 844 846 assert controller.instance_variable_get(:@before_filter_chain_aborted)