| 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 |
|---|
| 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"]) |
|---|
| 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 |
|---|
| | 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 | |
|---|