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

Changeset 3580

Show
Ignore:
Timestamp:
02/12/06 05:51:02 (3 years ago)
Author:
david
Message:

Stopped the massive bleeding of concerns into ActionController::Base. Base no longer knows about flash, filters, or components. This may well have introduced some instability, please do test with apps, especially the ones using components. [DHH]

Files:

Legend:

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

    r2247 r3580  
    3939require 'action_controller/rescue' 
    4040require 'action_controller/benchmarking' 
     41require 'action_controller/flash' 
    4142require 'action_controller/filters' 
    4243require 'action_controller/layout' 
    43 require 'action_controller/flash' 
    4444require 'action_controller/dependencies' 
    4545require 'action_controller/pagination' 
     
    4949require 'action_controller/cgi_process' 
    5050require 'action_controller/caching' 
    51 require 'action_controller/components' 
    5251require 'action_controller/verification' 
    5352require 'action_controller/streaming' 
    5453require 'action_controller/session_management' 
     54require 'action_controller/components' 
    5555require 'action_controller/macros/auto_complete' 
    5656require 'action_controller/macros/in_place_editing' 
     
    6060 
    6161ActionController::Base.class_eval do 
     62  include ActionController::Flash 
    6263  include ActionController::Filters 
    6364  include ActionController::Layout 
    64   include ActionController::Flash 
    6565  include ActionController::Benchmarking 
    6666  include ActionController::Rescue 
     
    7171  include ActionController::Cookies 
    7272  include ActionController::Caching 
    73   include ActionController::Components 
    7473  include ActionController::Verification 
    7574  include ActionController::Streaming 
    7675  include ActionController::SessionManagement 
     76  include ActionController::Components 
    7777  include ActionController::Macros::AutoComplete 
    7878  include ActionController::Macros::InPlaceEditing 
  • trunk/actionpack/lib/action_controller/base.rb

    r3576 r3580  
    306306    class << self 
    307307      # 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) 
    310310      end 
    311311       
     
    348348 
    349349    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_controller 
    353  
    354       # Create a new controller instance. 
    355       def initialize(parent_controller=nil) #:nodoc: 
    356         @parent_controller = parent_controller 
    357       end 
    358  
    359350      # Extracts the action_name from the request parameters and performs that action. 
    360351      def process(request, response, method = :perform_action, *arguments) #:nodoc: 
    361352        initialize_template_class(response) 
    362353        assign_shortcuts(request, response) 
    363  
    364         my_flash = flash # calling flash creates @flash 
    365         if my_parent = @parent_controller 
    366           # only discard flash if this controller isn't a component request controller 
    367           my_flash.discard 
    368         end 
    369  
    370354        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 
    376359        send(method, *arguments) 
    377         @response 
     360         
     361        return response 
    378362      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 
    386364      end 
    387365 
     
    471449      def controller_name 
    472450        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 
    473460      end 
    474461 
     
    669656      def render_to_string(options = nil, &block) #:doc: 
    670657        result = render(options, &block) 
     658 
    671659        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 
    674663        result 
    675664      end     
     
    887876 
    888877      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 
    892883      end 
    893884     
     
    922913        end 
    923914      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 
    924923 
    925924      def add_instance_variables_to_assigns 
     
    994993        path.to_s['/'] && self.class.controller_path.split('/')[-1] == path.split('/')[0] 
    995994      end 
     995 
     996      def process_cleanup 
     997        close_session 
     998      end 
    996999  end 
    9971000end 
  • trunk/actionpack/lib/action_controller/benchmarking.rb

    r3441 r3580  
    55  # package has been included, a separate timing section for database calls will be added as well. 
    66  module Benchmarking #:nodoc: 
    7     def self.append_features(base) 
    8       super 
     7    def self.included(base) 
    98      base.extend(ClassMethods) 
     9 
    1010      base.class_eval do 
    1111        alias_method :perform_action_without_benchmark, :perform_action 
  • trunk/actionpack/lib/action_controller/caching.rb

    r3533 r3580  
    99  # Note: To turn off all caching and sweeping, set Base.perform_caching = false. 
    1010  module Caching 
    11     def self.append_features(base) #:nodoc: 
    12       super 
     11    def self.included(base) #:nodoc: 
    1312      base.send(:include, Pages, Actions, Fragments, Sweeping) 
     13 
    1414      base.class_eval do 
    1515        @@perform_caching = true 
  • trunk/actionpack/lib/action_controller/components.rb

    r3577 r3580  
    3737  module Components 
    3838    def self.included(base) #:nodoc: 
     39      base.send :include, InstanceMethods 
    3940      base.extend(ClassMethods) 
    40       base.send(:include, InstanceMethods) 
    4141 
    4242      base.helper do 
     
    4545        end 
    4646      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 
    4759    end 
    4860 
    4961    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 
    5069      # Set the template root to be one directory behind the root dir of the controller. Examples: 
    5170      #   /code/weblog/components/admin/users_controller.rb with Admin::UsersController  
     
    6584 
    6685    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       
    6792      protected 
    6893        # Renders the component specified as the response for the current method 
     
    91116          request  = request_for_component(klass.controller_name, options) 
    92117          response = reuse_response ? @response : @response.dup 
    93          
    94           klass.process(request, response, self) 
     118 
     119          klass.process_with_components(request, response, self) 
    95120        end 
    96121         
     
    119144           
    120145          request 
    121         end 
     146        end 
    122147 
    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 
    133170    end 
    134171  end 
  • trunk/actionpack/lib/action_controller/filters.rb

    r3577 r3580  
    336336 
    337337    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 
    341340          alias_method :perform_action_without_filters, :perform_action 
    342341          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 
    344349      end 
    345350 
    346351      def perform_action_with_filters 
    347352        before_action_result = before_action 
     353 
    348354        unless before_action_result == false || performed? 
    349355          perform_action_without_filters 
    350356          after_action 
    351357        end 
     358 
    352359        @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) 
    353365      end 
    354366 
     
    369381          filters.each do |filter|  
    370382            next if action_exempted?(filter) 
     383 
    371384            filter_result = case 
    372385              when filter.is_a?(Symbol) 
     
    406419          end 
    407420        end 
     421 
     422        def process_cleanup_with_filters 
     423          process_cleanup_without_filters unless @before_filter_chain_aborted 
     424        end 
    408425    end 
    409426  end 
  • trunk/actionpack/lib/action_controller/flash.rb

    r3563 r3580  
    2525  # See docs on the FlashHash class for more details about the flash. 
    2626  module Flash 
     27    def self.included(base) 
     28      base.send :include, InstanceMethods 
    2729 
     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     
    2837    class FlashNow #:nodoc: 
    2938      def initialize flash 
     
    5362      end 
    5463       
    55       def update h #:nodoc: 
    56         h.keys.each{|k| discard k
     64      def update(h) #:nodoc: 
     65        h.keys.each{ |k| discard(k)
    5766        super 
    5867      end 
    5968       
    60       alias merge! update 
     69      alias :merge! :update 
    6170       
    62       def replace h #:nodoc: 
     71      def replace(h) #:nodoc: 
    6372        @used = {} 
    6473        super 
     
    125134    end 
    126135 
     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 
    127160 
    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 
    153167  end 
    154168end 
  • trunk/actionpack/lib/action_controller/session_management.rb

    r3563 r3580  
    77module ActionController #:nodoc: 
    88  module SessionManagement #:nodoc: 
    9     def self.append_features(base) 
    10       super 
     9    def self.included(base) 
    1110      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 
    1417    end 
    1518 
     
    111114 
    112115    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) 
    118117      process_without_session_management_support(request, response, method, *arguments) 
    119118    end 
    120119 
    121120    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 
    122130      # Clear cached associations in session data so they don't overflow 
    123131      # the database field.  Only applies to ActiveRecordStore since there 
    124132      # is not a standard way to iterate over session data. 
    125133      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') 
    127135          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) 
    129138            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) 
    133140            end 
    134141          end 
  • trunk/actionpack/test/controller/filters_test.rb

    r3563 r3580  
    190190  class MixedFilterController < PrependingController 
    191191    cattr_accessor :execution_log 
    192     def initialize(parent_controller=nil) 
    193       super(parent_controller) 
     192 
     193    def initialize 
    194194      @@execution_log = "" 
    195195    end