Skip to content

Instantly share code, notes, and snippets.

@richcollins
Created September 28, 2009 03:11
Show Gist options
  • Save richcollins/195116 to your computer and use it in GitHub Desktop.
Save richcollins/195116 to your computer and use it in GitHub Desktop.
render(options = nil, extra_options = {}, &block) #:doc:
raise DoubleRenderError, "Can only render or redirect once per action" if performed?
validate_render_arguments(options, extra_options, block_given?)
if options.nil?
options = { :template => default_template, :layout => true }
elsif options == :update
options = extra_options.merge({ :update => true })
elsif options.is_a?(String) || options.is_a?(Symbol)
case options.to_s.index('/')
when 0
extra_options[:file] = options
when nil
extra_options[:action] = options
else
extra_options[:template] = options
end
options = extra_options
elsif !options.is_a?(Hash)
extra_options[:partial] = options
options = extra_options
end
layout = pick_layout(options)
response.layout = layout.path_without_format_and_extension if layout
logger.info("Rendering template within #{layout.path_without_format_and_extension}") if logger && layout
if content_type = options[:content_type]
response.content_type = content_type.to_s
end
if location = options[:location]
response.headers["Location"] = url_for(location)
end
if options.has_key?(:text)
text = layout ? @template.render(options.merge(:text => options[:text], :layout => layout)) : options[:text]
render_for_text(text, options[:status])
else
if file = options[:file]
render_for_file(file, options[:status], layout, options[:locals] || {})
elsif template = options[:template]
render_for_file(template, options[:status], layout, options[:locals] || {})
elsif inline = options[:inline]
render_for_text(@template.render(options.merge(:layout => layout)), options[:status])
elsif action_name = options[:action]
render_for_file(default_template(action_name.to_s), options[:status], layout)
elsif xml = options[:xml]
response.content_type ||= Mime::XML
render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
elsif js = options[:js]
response.content_type ||= Mime::JS
render_for_text(js, options[:status])
elsif options.include?(:json)
json = options[:json]
json = ActiveSupport::JSON.encode(json) unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type ||= Mime::JSON
render_for_text(json, options[:status])
elsif options[:partial]
options[:partial] = default_template_name if options[:partial] == true
if layout
render_for_text(@template.render(:text => @template.render(options), :layout => layout), options[:status])
else
render_for_text(@template.render(options), options[:status])
end
elsif options[:update]
@template.send(:_evaluate_assigns_and_ivars)
generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block)
response.content_type = Mime::JS
render_for_text(generator.to_s, options[:status])
elsif options[:nothing]
render_for_text(nil, options[:status])
else
render_for_file(default_template, options[:status], layout)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment