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

Changeset 3576

Show
Ignore:
Timestamp:
02/12/06 00:46:01 (3 years ago)
Author:
david
Message:

The components module should also contain the options that pertain to it, so collect it all with ClassMethods and InstanceMethods

Files:

Legend:

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

    r3563 r3576  
    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: 
     308      def process(request, response, parent_controller = nil) #:nodoc: 
    309309        new(parent_controller).process(request, response) 
    310310      end 
     
    329329          @controller_path = components.map { |name| name.underscore }.join('/') 
    330330        end 
     331 
    331332        @controller_path 
    332333      end 
     
    334335      # Return an array containing the names of public methods that have been marked hidden from the action processor. 
    335336      # By default, all methods defined in ActionController::Base and included modules are hidden. 
    336       # More methods can be hidden using +hide_actions+
     337      # More methods can be hidden using <tt>hide_actions</tt>
    337338      def hidden_actions 
    338339        write_inheritable_attribute(:hidden_actions, ActionController::Base.public_instance_methods) unless read_inheritable_attribute(:hidden_actions) 
     
    342343      # Hide each of the given methods from being callable as actions. 
    343344      def hide_action(*names) 
    344         write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect {|n| n.to_s}) 
    345       end 
    346  
    347       # Set the template root to be one directory behind the root dir of the controller. Examples: 
    348       #   /code/weblog/components/admin/users_controller.rb with Admin::UsersController  
    349       #     will use /code/weblog/components as template root  
    350       #     and find templates in /code/weblog/components/admin/users/ 
    351       # 
    352       #   /code/weblog/components/admin/parties/users_controller.rb with Admin::Parties::UsersController  
    353       #     will also use /code/weblog/components as template root  
    354       #     and find templates in /code/weblog/components/admin/parties/users/ 
    355       def uses_component_template_root 
    356         path_of_calling_controller = File.dirname(caller[0].split(/:\d+:/).first) 
    357         path_of_controller_root    = path_of_calling_controller.sub(/#{controller_path.split("/")[0..-2]}$/, "") # " (for ruby-mode) 
    358         self.template_root = path_of_controller_root 
     345        write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s }) 
    359346      end 
    360347    end 
  • trunk/actionpack/lib/action_controller/components.rb

    r3563 r3576  
    2929  # would work as well and be slightly faster. 
    3030  module Components 
    31     def self.append_features(base) #:nodoc: 
    32       super 
     31    def self.included(base) #:nodoc: 
     32      base.extend(ClassMethods) 
     33      base.send(:include, InstanceMethods) 
     34 
    3335      base.helper do 
    3436        def render_component(options)  
     
    3840    end 
    3941 
    40     protected 
    41       # Renders the component specified as the response for the current method 
    42       def render_component(options) #:doc: 
    43         component_logging(options) do 
    44           render_text(component_response(options, true).body, response.headers["Status"]) 
    45         end 
     42    module ClassMethods 
     43      # Set the template root to be one directory behind the root dir of the controller. Examples: 
     44      #   /code/weblog/components/admin/users_controller.rb with Admin::UsersController  
     45      #     will use /code/weblog/components as template root  
     46      #     and find templates in /code/weblog/components/admin/users/ 
     47      # 
     48      #   /code/weblog/components/admin/parties/users_controller.rb with Admin::Parties::UsersController  
     49      #     will also use /code/weblog/components as template root  
     50      #     and find templates in /code/weblog/components/admin/parties/users/ 
     51      def uses_component_template_root 
     52        path_of_calling_controller = File.dirname(caller[0].split(/:\d+:/).first) 
     53        path_of_controller_root    = path_of_calling_controller.sub(/#{controller_path.split("/")[0..-2]}$/, "") # " (for ruby-mode) 
     54        self.template_root = path_of_controller_root 
    4655      end 
     56    end 
    4757 
    48       # Returns the component response as a string 
    49       def render_component_as_string(options) #:doc: 
    50         component_logging(options) do 
    51           response = component_response(options, false) 
    52           if redirected = response.redirected_to 
    53             render_component_as_string redirected 
    54           else 
    55             response.body 
     58    module InstanceMethods 
     59      protected 
     60        # Renders the component specified as the response for the current method 
     61        def render_component(options) #:doc: 
     62          component_logging(options) do 
     63            render_text(component_response(options, true).body, response.headers["Status"]) 
    5664          end 
    57        end 
    58       end 
    59    
    60     private 
    61        def component_response(options, reuse_response) 
    62          c_class = component_class(options) 
    63          c_request = request_for_component(c_class.controller_name, options) 
    64          c_response = reuse_response ? @response : @response.dup 
    65          c_class.process(c_request, c_response, self) 
    66        end 
    67   
    68        # determine the controller class for the component request 
    69        def component_class(options) 
    70          if controller = options[:controller] 
    71            if controller.is_a? Class 
    72              controller 
    73            else 
    74              "#{controller.camelize}Controller".constantize 
    75            end 
    76          else 
    77            self.class 
    78          end 
    79        end 
    80   
    81        # Create a new request object based on the current request. 
    82        # The new request inherits the session from the current request, 
    83        # bypassing any session options set for the component controller's class 
    84        def request_for_component(controller_name, options) 
    85          sub_request = @request.dup 
    86          sub_request.session = @request.session 
    87          sub_request.instance_variable_set(:@parameters, 
    88              (options[:params] || {}).with_indifferent_access.regular_update( 
    89                 "controller" => controller_name, "action" => options[:action], "id" => options[:id]) 
    90          ) 
    91          sub_request 
    9265        end 
    9366 
     67        # Returns the component response as a string 
     68        def render_component_as_string(options) #:doc: 
     69          component_logging(options) do 
     70            response = component_response(options, false) 
     71            if redirected = response.redirected_to 
     72              render_component_as_string redirected 
     73            else 
     74              response.body 
     75            end 
     76         end 
     77        end 
     78   
     79      private 
     80         def component_response(options, reuse_response) 
     81           c_class = component_class(options) 
     82           c_request = request_for_component(c_class.controller_name, options) 
     83           c_response = reuse_response ? @response : @response.dup 
     84           c_class.process(c_request, c_response, self) 
     85         end 
     86  
     87         # determine the controller class for the component request 
     88         def component_class(options) 
     89           if controller = options[:controller] 
     90             if controller.is_a? Class 
     91               controller 
     92             else 
     93               "#{controller.camelize}Controller".constantize 
     94             end 
     95           else 
     96             self.class 
     97           end 
     98         end 
     99  
     100         # Create a new request object based on the current request. 
     101         # The new request inherits the session from the current request, 
     102         # bypassing any session options set for the component controller's class 
     103         def request_for_component(controller_name, options) 
     104           sub_request = @request.dup 
     105           sub_request.session = @request.session 
     106           sub_request.instance_variable_set(:@parameters, 
     107               (options[:params] || {}).with_indifferent_access.regular_update( 
     108                  "controller" => controller_name, "action" => options[:action], "id" => options[:id]) 
     109           ) 
     110           sub_request 
     111          end 
     112 
    94113       
    95       def component_logging(options) 
    96         unless logger then yield else 
    97           logger.info("Start rendering component (#{options.inspect}): ") 
    98           result = yield 
    99           logger.info("\n\nEnd of component rendering") 
    100           result 
     114        def component_logging(options) 
     115          unless logger then yield else 
     116            logger.info("Start rendering component (#{options.inspect}): ") 
     117            result = yield 
     118            logger.info("\n\nEnd of component rendering") 
     119            result 
     120          end 
    101121        end 
    102       end 
     122    end 
    103123  end 
    104124end