Bring normalize behavior to AbstractController::Rendering

This commit is contained in:
José Valim
2010-01-20 14:21:27 +01:00
parent 8b9bfbe225
commit 8e2fd54b19
5 changed files with 37 additions and 39 deletions

View File

@@ -34,7 +34,7 @@ module AbstractController
end
end
def render(options)
def render(*args)
Thread.current[:format_locale_key] = HashKey.get(self.class, formats, I18n.locale)
super
end

View File

@@ -40,12 +40,13 @@ module AbstractController
# Mostly abstracts the fact that calling render twice is a DoubleRenderError.
# Delegates render_to_body and sticks the result in self.response_body.
def render(*args)
def render(*args, &block)
if response_body
raise AbstractController::DoubleRenderError, "Can only render or redirect once per action"
end
self.response_body = render_to_body(*args)
options = _normalize_options(*args, &block)
self.response_body = render_to_body(options)
end
# Raw rendering of a template to a Rack-compatible body.
@@ -69,7 +70,8 @@ module AbstractController
# render_to_body into a String.
#
# :api: plugin
def render_to_string(options = {})
def render_to_string(*args)
options = _normalize_options(*args)
AbstractController::Rendering.body_to_s(render_to_body(options))
end
@@ -96,6 +98,20 @@ module AbstractController
_view_paths
end
# Normalize options, by converting render "foo" to render :template => "foo"
# and render "/foo" to render :file => "/foo".
def _normalize_options(action=nil, options={})
case action
when Hash
options, action = action, nil
when String
key = (action.index("/") == 0 ? :file : :template)
options.merge!(key => action)
end
options
end
# Return a string representation of a Rack-compatible response body.
def self.body_to_s(body)
if body.respond_to?(:to_str)

View File

@@ -74,17 +74,14 @@ module ActionController
@subclasses ||= []
end
def _normalize_options(action = nil, options = {}, &blk)
if action.is_a?(Hash)
options, action = action, nil
elsif action.is_a?(String) || action.is_a?(Symbol)
key = case action = action.to_s
when %r{^/} then :file
when %r{/} then :template
else :action
end
options.merge! key => action
elsif action
def _normalize_options(action=nil, options={}, &blk)
case action
when NilClass
when Hash, String
options = super
when Symbol
options.merge! :action => action
else
options.merge! :partial => action
end
@@ -99,15 +96,5 @@ module ActionController
options[:update] = blk if block_given?
options
end
def render(action = nil, options = {}, &blk)
options = _normalize_options(action, options, &blk)
super(options)
end
def render_to_string(action = nil, options = {}, &blk)
options = _normalize_options(action, options, &blk)
super(options)
end
end
end

View File

@@ -32,18 +32,12 @@ module ActionController
end
end
def render(*args, &block)
if logger
render_output = nil
self.view_runtime = cleanup_view_runtime do
Benchmark.ms { render_output = super }
end
render_output
else
super
def render(*args)
render_output = nil
self.view_runtime = cleanup_view_runtime do
Benchmark.ms { render_output = super }
end
render_output
end
def send_file(path, options={})

View File

@@ -12,9 +12,10 @@ module ActionController
super
end
def render(options)
super
self.content_type ||= options[:_template].mime_type.to_s
def render(*args)
args << {} unless args.last.is_a?(Hash)
super(*args)
self.content_type ||= args.last[:_template].mime_type.to_s
response_body
end