Changeset 3580
- Timestamp:
- 02/12/06 05:51:02 (3 years ago)
- Files:
-
- trunk/actionpack/lib/action_controller.rb (modified) (4 diffs)
- trunk/actionpack/lib/action_controller/base.rb (modified) (7 diffs)
- trunk/actionpack/lib/action_controller/benchmarking.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/caching.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/components.rb (modified) (5 diffs)
- trunk/actionpack/lib/action_controller/filters.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/flash.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/session_management.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/filters_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller.rb
r2247 r3580 39 39 require 'action_controller/rescue' 40 40 require 'action_controller/benchmarking' 41 require 'action_controller/flash' 41 42 require 'action_controller/filters' 42 43 require 'action_controller/layout' 43 require 'action_controller/flash'44 44 require 'action_controller/dependencies' 45 45 require 'action_controller/pagination' … … 49 49 require 'action_controller/cgi_process' 50 50 require 'action_controller/caching' 51 require 'action_controller/components'52 51 require 'action_controller/verification' 53 52 require 'action_controller/streaming' 54 53 require 'action_controller/session_management' 54 require 'action_controller/components' 55 55 require 'action_controller/macros/auto_complete' 56 56 require 'action_controller/macros/in_place_editing' … … 60 60 61 61 ActionController::Base.class_eval do 62 include ActionController::Flash 62 63 include ActionController::Filters 63 64 include ActionController::Layout 64 include ActionController::Flash65 65 include ActionController::Benchmarking 66 66 include ActionController::Rescue … … 71 71 include ActionController::Cookies 72 72 include ActionController::Caching 73 include ActionController::Components74 73 include ActionController::Verification 75 74 include ActionController::Streaming 76 75 include ActionController::SessionManagement 76 include ActionController::Components 77 77 include ActionController::Macros::AutoComplete 78 78 include ActionController::Macros::InPlaceEditing trunk/actionpack/lib/action_controller/base.rb
r3576 r3580 306 306 class << self 307 307 # Factory for the standard create, process loop where the controller is discarded after processing. 308 def process(request, response , parent_controller = nil) #:nodoc:309 new (parent_controller).process(request, response)308 def process(request, response) #:nodoc: 309 new.process(request, response) 310 310 end 311 311 … … 348 348 349 349 public 350 # If this controller was instantiated to process a component request,351 # +parent_controller+ points to the instantiator of this controller.352 attr_reader :parent_controller353 354 # Create a new controller instance.355 def initialize(parent_controller=nil) #:nodoc:356 @parent_controller = parent_controller357 end358 359 350 # Extracts the action_name from the request parameters and performs that action. 360 351 def process(request, response, method = :perform_action, *arguments) #:nodoc: 361 352 initialize_template_class(response) 362 353 assign_shortcuts(request, response) 363 364 my_flash = flash # calling flash creates @flash365 if my_parent = @parent_controller366 # only discard flash if this controller isn't a component request controller367 my_flash.discard368 end369 370 354 initialize_current_url 371 @action_name = params['action'] || 'index' 372 @variables_added = nil 373 @before_filter_chain_aborted = false 374 375 log_processing if logger 355 action_name(:refresh) 356 forget_variables_added_to_assigns 357 358 log_processing 376 359 send(method, *arguments) 377 @response 360 361 return response 378 362 ensure 379 unless my_parent 380 unless @before_filter_chain_aborted 381 my_flash.sweep 382 clear_persistent_model_associations 383 end 384 close_session 385 end 363 process_cleanup 386 364 end 387 365 … … 471 449 def controller_name 472 450 self.class.controller_name 451 end 452 453 # Returns the name of the current action 454 def action_name(refresh = false) 455 if @action_name.nil? || refresh 456 @action_name = (params['action'] || 'index') 457 end 458 459 @action_name 473 460 end 474 461 … … 669 656 def render_to_string(options = nil, &block) #:doc: 670 657 result = render(options, &block) 658 671 659 erase_render_results 672 @variables_added = nil 673 @template.instance_variable_set("@assigns_added", nil) 660 forget_variables_added_to_assigns 661 reset_variables_added_to_assigns 662 674 663 result 675 664 end … … 887 876 888 877 def log_processing 889 logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]" 890 logger.info " Session ID: #{@session.session_id}" if @session and @session.respond_to?(:session_id) 891 logger.info " Parameters: #{@params.inspect}" 878 if logger 879 logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]" 880 logger.info " Session ID: #{@session.session_id}" if @session and @session.respond_to?(:session_id) 881 logger.info " Parameters: #{@params.inspect}" 882 end 892 883 end 893 884 … … 922 913 end 923 914 end 915 916 def forget_variables_added_to_assigns 917 @variables_added = nil 918 end 919 920 def reset_variables_added_to_assigns 921 @template.instance_variable_set("@assigns_added", nil) 922 end 924 923 925 924 def add_instance_variables_to_assigns … … 994 993 path.to_s['/'] && self.class.controller_path.split('/')[-1] == path.split('/')[0] 995 994 end 995 996 def process_cleanup 997 close_session 998 end 996 999 end 997 1000 end trunk/actionpack/lib/action_controller/benchmarking.rb
r3441 r3580 5 5 # package has been included, a separate timing section for database calls will be added as well. 6 6 module Benchmarking #:nodoc: 7 def self.append_features(base) 8 super 7 def self.included(base) 9 8 base.extend(ClassMethods) 9 10 10 base.class_eval do 11 11 alias_method :perform_action_without_benchmark, :perform_action trunk/actionpack/lib/action_controller/caching.rb
r3533 r3580 9 9 # Note: To turn off all caching and sweeping, set Base.perform_caching = false. 10 10 module Caching 11 def self.append_features(base) #:nodoc: 12 super 11 def self.included(base) #:nodoc: 13 12 base.send(:include, Pages, Actions, Fragments, Sweeping) 13 14 14 base.class_eval do 15 15 @@perform_caching = true trunk/actionpack/lib/action_controller/components.rb
r3577 r3580 37 37 module Components 38 38 def self.included(base) #:nodoc: 39 base.send :include, InstanceMethods 39 40 base.extend(ClassMethods) 40 base.send(:include, InstanceMethods)41 41 42 42 base.helper do … … 45 45 end 46 46 end 47 48 base.class_eval do 49 alias_method :process_cleanup_without_components, :process_cleanup 50 alias_method :process_cleanup, :process_cleanup_with_components 51 52 alias_method :set_session_options_without_components, :set_session_options 53 alias_method :set_session_options, :set_session_options_with_components 54 end 55 56 # If this controller was instantiated to process a component request, 57 # +parent_controller+ points to the instantiator of this controller. 58 base.send :attr_accessor, :parent_controller 47 59 end 48 60 49 61 module ClassMethods 62 # Track parent controller to identify component requests 63 def process_with_components(request, response, parent_controller = nil) #:nodoc: 64 controller = new 65 controller.parent_controller = parent_controller 66 controller.process(request, response) 67 end 68 50 69 # Set the template root to be one directory behind the root dir of the controller. Examples: 51 70 # /code/weblog/components/admin/users_controller.rb with Admin::UsersController … … 65 84 66 85 module InstanceMethods 86 # Extracts the action_name from the request parameters and performs that action. 87 def process_with_components(request, response, method = :perform_action, *arguments) #:nodoc: 88 flash.discard if component_request? 89 process_without_components(request, response, method, *arguments) 90 end 91 67 92 protected 68 93 # Renders the component specified as the response for the current method … … 91 116 request = request_for_component(klass.controller_name, options) 92 117 response = reuse_response ? @response : @response.dup 93 94 klass.process (request, response, self)118 119 klass.process_with_components(request, response, self) 95 120 end 96 121 … … 119 144 120 145 request 121 end146 end 122 147 123 def component_logging(options) 124 if logger 125 logger.info "Start rendering component (#{options.inspect}): " 126 result = yield 127 logger.info "\n\nEnd of component rendering" 128 result 129 else 130 yield 131 end 132 end 148 def component_logging(options) 149 if logger 150 logger.info "Start rendering component (#{options.inspect}): " 151 result = yield 152 logger.info "\n\nEnd of component rendering" 153 result 154 else 155 yield 156 end 157 end 158 159 def component_request? 160 !@parent_controller.nil? 161 end 162 163 def set_session_options_with_components(request) 164 set_session_options_without_components(request) unless component_request? 165 end 166 167 def process_cleanup_with_components 168 process_cleanup_without_components unless component_request? 169 end 133 170 end 134 171 end trunk/actionpack/lib/action_controller/filters.rb
r3577 r3580 336 336 337 337 module InstanceMethods # :nodoc: 338 def self.append_features(base) 339 super 340 base.class_eval { 338 def self.included(base) 339 base.class_eval do 341 340 alias_method :perform_action_without_filters, :perform_action 342 341 alias_method :perform_action, :perform_action_with_filters 343 } 342 343 alias_method :process_without_filters, :process 344 alias_method :process, :process_with_filters 345 346 alias_method :process_cleanup_without_filters, :process_cleanup 347 alias_method :process_cleanup, :process_cleanup_with_filters 348 end 344 349 end 345 350 346 351 def perform_action_with_filters 347 352 before_action_result = before_action 353 348 354 unless before_action_result == false || performed? 349 355 perform_action_without_filters 350 356 after_action 351 357 end 358 352 359 @before_filter_chain_aborted = (before_action_result == false) 360 end 361 362 def process_with_filters(request, response, method = :perform_action, *arguments) #:nodoc: 363 @before_filter_chain_aborted = false 364 process_without_filters(request, response, method, *arguments) 353 365 end 354 366 … … 369 381 filters.each do |filter| 370 382 next if action_exempted?(filter) 383 371 384 filter_result = case 372 385 when filter.is_a?(Symbol) … … 406 419 end 407 420 end 421 422 def process_cleanup_with_filters 423 process_cleanup_without_filters unless @before_filter_chain_aborted 424 end 408 425 end 409 426 end trunk/actionpack/lib/action_controller/flash.rb
r3563 r3580 25 25 # See docs on the FlashHash class for more details about the flash. 26 26 module Flash 27 def self.included(base) 28 base.send :include, InstanceMethods 27 29 30 base.class_eval do 31 alias_method :process_cleanup_without_flash, :process_cleanup 32 alias_method :process_cleanup, :process_cleanup_with_flash 33 end 34 end 35 36 28 37 class FlashNow #:nodoc: 29 38 def initialize flash … … 53 62 end 54 63 55 def update h#:nodoc:56 h.keys.each{ |k| discard k}64 def update(h) #:nodoc: 65 h.keys.each{ |k| discard(k) } 57 66 super 58 67 end 59 68 60 alias merge!update69 alias :merge! :update 61 70 62 def replace h#:nodoc:71 def replace(h) #:nodoc: 63 72 @used = {} 64 73 super … … 125 134 end 126 135 136 module InstanceMethods 137 def process_cleanup_with_flash 138 process_cleanup_without_flash 139 flash.sweep 140 end 141 142 protected 143 # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or 144 # <tt>flash["notice"] = "hello"</tt> to put a new one. 145 # Note that if sessions are disabled only flash.now will work. 146 def flash #:doc: 147 @flash ||= 148 if @parent_controller 149 @parent_controller.flash 150 elsif @session.is_a?(Hash) 151 # @session is a Hash, if sessions are disabled 152 # we don't put the flash in the session in this case 153 FlashHash.new 154 else 155 # otherwise, @session is a CGI::Session or a TestSession 156 # so make sure it gets retrieved from/saved to session storage after request processing 157 @session["flash"] ||= FlashHash.new 158 end 159 end 127 160 128 protected 129 # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or 130 # <tt>flash["notice"] = "hello"</tt> to put a new one. 131 # Note that if sessions are disabled only flash.now will work. 132 def flash #:doc: 133 @flash ||= 134 if @parent_controller 135 @parent_controller.flash 136 elsif @session.is_a?(Hash) 137 # @session is a Hash, if sessions are disabled 138 # we don't put the flash in the session in this case 139 FlashHash.new 140 else 141 # otherwise, @session is a CGI::Session or a TestSession 142 # so make sure it gets retrieved from/saved to session storage after request processing 143 @session["flash"] ||= FlashHash.new 144 end 145 end 146 147 # deprecated. use <tt>flash.keep</tt> instead 148 def keep_flash #:doc: 149 warn 'keep_flash is deprecated; use flash.keep instead.' 150 flash.keep 151 end 152 161 # deprecated. use <tt>flash.keep</tt> instead 162 def keep_flash #:doc: 163 warn 'keep_flash is deprecated; use flash.keep instead.' 164 flash.keep 165 end 166 end 153 167 end 154 168 end trunk/actionpack/lib/action_controller/session_management.rb
r3563 r3580 7 7 module ActionController #:nodoc: 8 8 module SessionManagement #:nodoc: 9 def self.append_features(base) 10 super 9 def self.included(base) 11 10 base.extend(ClassMethods) 12 base.send(:alias_method, :process_without_session_management_support, :process) 13 base.send(:alias_method, :process, :process_with_session_management_support) 11 12 base.send :alias_method, :process_without_session_management_support, :process 13 base.send :alias_method, :process, :process_with_session_management_support 14 15 base.send :alias_method, :process_cleanup_without_session_management_support, :process_cleanup 16 base.send :alias_method, :process_cleanup, :process_cleanup_with_session_management_support 14 17 end 15 18 … … 111 114 112 115 def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc: 113 unless @parent_controller 114 # only determine session options if this isn't a controller created for component request processing 115 action = request.parameters["action"] || "index" 116 request.session_options = self.class.session_options_for(request, action) 117 end 116 set_session_options(request) 118 117 process_without_session_management_support(request, response, method, *arguments) 119 118 end 120 119 121 120 private 121 def set_session_options(request) 122 request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index") 123 end 124 125 def process_cleanup_with_session_management_support 126 process_cleanup_without_session_management_support 127 clear_persistent_model_associations 128 end 129 122 130 # Clear cached associations in session data so they don't overflow 123 131 # the database field. Only applies to ActiveRecordStore since there 124 132 # is not a standard way to iterate over session data. 125 133 def clear_persistent_model_associations #:doc: 126 if defined?(@session) and@session.instance_variables.include?('@data')134 if defined?(@session) && @session.instance_variables.include?('@data') 127 135 session_data = @session.instance_variable_get('@data') 128 if session_data and session_data.respond_to?(:each_value) 136 137 if session_data && session_data.respond_to?(:each_value) 129 138 session_data.each_value do |obj| 130 if obj.respond_to?(:clear_association_cache) 131 obj.clear_association_cache 132 end 139 obj.clear_association_cache if obj.respond_to?(:clear_association_cache) 133 140 end 134 141 end trunk/actionpack/test/controller/filters_test.rb
r3563 r3580 190 190 class MixedFilterController < PrependingController 191 191 cattr_accessor :execution_log 192 def initialize(parent_controller=nil) 193 super(parent_controller)192 193 def initialize 194 194 @@execution_log = "" 195 195 end