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

Changeset 3577

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

Add caution and restyle components

Files:

Legend:

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

    r3352 r3577  
    303303 
    304304      # ensures that the passed record is valid by active record standards. returns the error messages if not 
    305       def assert_valid(record)                                    
     305      def assert_valid(record) 
    306306        clean_backtrace do 
    307307          assert record.valid?, record.errors.full_messages.join("\n") 
  • trunk/actionpack/lib/action_controller/components.rb

    r3576 r3577  
    11module ActionController #:nodoc: 
    2   # Components allows you to call other actions for their rendered response while executing another action. You can either delegate 
     2  # Components allow you to call other actions for their rendered response while executing another action. You can either delegate 
    33  # the entire response rendering or you can mix a partial response in with your other content. 
    44  # 
     
    77  #     def delegate_action 
    88  #       do_other_stuff_before_hello_world 
    9   #       render_component :controller => "greeter",  :action => "hello_world", :params => { "person" => "david" } 
     9  #       render_component :controller => "greeter",  :action => "hello_world", :params => { :person => "david" } 
    1010  #     end 
    1111  #   end 
     
    1313  #   class GreeterController < ActionController::Base 
    1414  #     def hello_world 
    15   #       render_text "#{@params['person']} says, Hello World!" 
     15  #       render :text => "#{params[:person]} says, Hello World!" 
    1616  #     end 
    1717  #   end 
     
    2323  # 
    2424  # It is also possible to specify the controller as a class constant, bypassing the inflector 
    25   # code to compute the controller class at runtime. Therefore, 
     25  # code to compute the controller class at runtime: 
    2626  #  
    2727  # <%= render_component :controller => GreeterController, :action => "hello_world" %> 
    28   #  
    29   # would work as well and be slightly faster. 
     28  # 
     29  # == When to use components 
     30  # 
     31  # Components should be used with care. They're significantly slower than simply splitting reusable parts into partials and 
     32  # conceptually more complicated. Don't use components as a way of separating concerns inside a single application. Instead, 
     33  # reserve components to those rare cases where you truly have reusable view and controller elements that can be employed  
     34  # across many applications at once. 
     35  # 
     36  # So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters. 
    3037  module Components 
    3138    def self.included(base) #:nodoc: 
     
    5259        path_of_calling_controller = File.dirname(caller[0].split(/:\d+:/).first) 
    5360        path_of_controller_root    = path_of_calling_controller.sub(/#{controller_path.split("/")[0..-2]}$/, "") # " (for ruby-mode) 
     61 
    5462        self.template_root = path_of_controller_root 
    5563      end 
     
    6977          component_logging(options) do 
    7078            response = component_response(options, false) 
     79 
    7180            if redirected = response.redirected_to 
    72               render_component_as_string redirected 
     81              render_component_as_string(redirected) 
    7382            else 
    7483              response.body 
    7584            end 
     85          end 
     86        end 
     87 
     88      private 
     89        def component_response(options, reuse_response) 
     90          klass    = component_class(options) 
     91          request  = request_for_component(klass.controller_name, options) 
     92          response = reuse_response ? @response : @response.dup 
     93         
     94          klass.process(request, response, self) 
     95        end 
     96         
     97        # determine the controller class for the component request 
     98        def component_class(options) 
     99          if controller = options[:controller] 
     100            controller.is_a?(Class) ? controller : "#{controller.camelize}Controller".constantize 
     101          else 
     102            self.class 
     103          end 
     104        end 
     105         
     106        # Create a new request object based on the current request. 
     107        # The new request inherits the session from the current request, 
     108        # bypassing any session options set for the component controller's class 
     109        def request_for_component(controller_name, options) 
     110          request         = @request.dup 
     111          request.session = @request.session 
     112         
     113          request.instance_variable_set( 
     114            :@parameters, 
     115            (options[:params] || {}).with_indifferent_access.regular_update( 
     116              "controller" => controller_name, "action" => options[:action], "id" => options[:id] 
     117            ) 
     118          ) 
     119           
     120          request 
    76121         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 
     122 
     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 
    95129           else 
    96              self.class 
     130             yield 
    97131           end 
    98132         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  
    113        
    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 
    121         end 
    122133    end 
    123134  end 
  • trunk/actionpack/lib/action_controller/filters.rb

    r3563 r3577  
    11module ActionController #:nodoc: 
    22  module Filters #:nodoc: 
    3     def self.append_features(base) 
    4       super 
     3    def self.included(base) 
    54      base.extend(ClassMethods) 
    65      base.send(:include, ActionController::Filters::InstanceMethods)