aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/components.rb
blob: d6ee700b094e6be4e9f41efbbd68bfe684e349d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module ActionController #:nodoc:
  # TODO: Cookies and session variables set in render_component that happens inside a view should be copied over.
  module Components #:nodoc:
    def self.append_features(base)
      super
      base.helper do
        def render_component(options) 
          @controller.logger.info("Start rendering component (#{options.inspect}): ")
          result = @controller.send(:component_response, options).body
          @controller.logger.info("\n\nEnd of component rendering")
          return result
        end
      end
    end

    protected
      def render_component(options = {}) #:doc:
        response = component_response(options, true)
        logger.info "Start rendering component (#{options.inspect}): "
        result = render_text(response.body, response.headers["Status"])
        logger.info("\n\nEnd of component rendering")
        return result
      end
  
    private
      def component_response(options, reuse_response = false)
        component_class(options).process(component_request(options), reuse_response ? @response : component_response)
      end
    
      def component_class(options)
        options[:controller] ? (options[:controller].camelize + "Controller").constantize : self.class
      end
      
      def component_request(options)
        component_request = Marshal::load(Marshal::dump(@request))
        component_request.send(
          :instance_variable_set, :@parameters, 
          (options[:params] || {}).merge({ "controller" => options[:controller], "action" => options[:action] })
        )
        return component_request
      end
      
      def component_response
        Marshal::load(Marshal::dump(@response))
      end
  end
end