2005-02-19 15:29:55 -05:00
|
|
|
module ActionController #:nodoc:
|
2005-02-23 20:29:43 -05:00
|
|
|
# Components allows you to call other actions for their rendered response while execution another action. You can either delegate
|
|
|
|
# the entire response rendering or you can mix a partial response in with your other content.
|
|
|
|
#
|
|
|
|
# class WeblogController < ActionController::Base
|
|
|
|
# # Performs a method and then lets hello_world output its render
|
|
|
|
# def delegate_action
|
|
|
|
# do_other_stuff_before_hello_world
|
2005-03-06 12:27:11 -05:00
|
|
|
# render_component :controller => "greeter", :action => "hello_world", :params => { "person" => "david" }
|
2005-02-23 20:29:43 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class GreeterController < ActionController::Base
|
|
|
|
# def hello_world
|
2005-03-06 12:27:11 -05:00
|
|
|
# render_text "#{@params['person']} says, Hello World!"
|
2005-02-23 20:29:43 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# The same can be done in a view to do a partial rendering:
|
|
|
|
#
|
|
|
|
# Let's see a greeting:
|
|
|
|
# <%= render_component :controller => "greeter", :action => "hello_world" %>
|
|
|
|
module Components
|
|
|
|
def self.append_features(base) #:nodoc:
|
2005-02-19 15:29:55 -05:00
|
|
|
super
|
2005-02-20 08:50:13 -05:00
|
|
|
base.helper do
|
|
|
|
def render_component(options)
|
2005-02-23 10:26:48 -05:00
|
|
|
@controller.send(:render_component_as_string, options)
|
2005-02-20 08:50:13 -05:00
|
|
|
end
|
|
|
|
end
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2005-02-23 20:29:43 -05:00
|
|
|
# Renders the component specified as the response for the current method
|
2005-02-19 15:29:55 -05:00
|
|
|
def render_component(options = {}) #:doc:
|
2005-02-23 10:26:48 -05:00
|
|
|
component_logging(options) { render_text(component_response(options).body, response.headers["Status"]) }
|
|
|
|
end
|
|
|
|
|
2005-02-23 20:29:43 -05:00
|
|
|
# Returns the component response as a string
|
2005-02-23 10:26:48 -05:00
|
|
|
def render_component_as_string(options) #:doc:
|
|
|
|
component_logging(options) { component_response(options, false).body }
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2005-02-22 12:10:00 -05:00
|
|
|
def component_response(options, reuse_response = true)
|
2005-09-27 07:02:11 -04:00
|
|
|
c = component_class(options)
|
|
|
|
c.after_filter {|c| flash.keep }
|
|
|
|
c.process(request_for_component(options), reuse_response ? @response : response_for_component)
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def component_class(options)
|
2005-02-19 19:02:02 -05:00
|
|
|
options[:controller] ? (options[:controller].camelize + "Controller").constantize : self.class
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
|
|
|
|
2005-02-22 11:58:45 -05:00
|
|
|
def request_for_component(options)
|
2005-02-22 12:11:34 -05:00
|
|
|
request_for_component = @request.dup
|
2005-02-22 11:58:45 -05:00
|
|
|
request_for_component.send(
|
2005-02-20 08:50:13 -05:00
|
|
|
:instance_variable_set, :@parameters,
|
2005-03-10 20:55:18 -05:00
|
|
|
(options[:params] || {}).merge({ "controller" => options[:controller], "action" => options[:action], "id" => options[:id] }).with_indifferent_access
|
2005-02-20 08:50:13 -05:00
|
|
|
)
|
2005-02-22 11:58:45 -05:00
|
|
|
return request_for_component
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
2005-02-22 09:14:42 -05:00
|
|
|
|
2005-02-22 11:58:45 -05:00
|
|
|
def response_for_component
|
2005-02-22 12:10:00 -05:00
|
|
|
@response.dup
|
2005-02-22 09:14:42 -05:00
|
|
|
end
|
2005-02-23 10:26:48 -05:00
|
|
|
|
|
|
|
def component_logging(options)
|
|
|
|
logger.info("Start rendering component (#{options.inspect}): ") unless logger.nil?
|
|
|
|
result = yield
|
|
|
|
logger.info("\n\nEnd of component rendering") unless logger.nil?
|
|
|
|
return result
|
|
|
|
end
|
2005-02-19 15:29:55 -05:00
|
|
|
end
|
|
|
|
end
|