Skip to content

Commit

Permalink
Set the response content type to that of found template if not explic…
Browse files Browse the repository at this point in the history
…itly set elsewhere [#444 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
tomafro authored and josh committed Jul 19, 2008
1 parent d2ccb85 commit c3d1fda
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -379,6 +379,12 @@ def assign_variables_from_controller
@assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end

def set_controller_content_type(content_type)
if controller.respond_to?(:response)
controller.response.content_type ||= content_type
end
end

def execute(method, local_assigns = {})
send(method, local_assigns) do |*names|
instance_variable_get "@content_for_#{names.first || 'layout'}"
Expand Down
5 changes: 4 additions & 1 deletion actionpack/lib/action_view/renderable.rb
Expand Up @@ -24,10 +24,13 @@ def compiled_source
memoize :compiled_source

def render(view, local_assigns = {})
compile(local_assigns)

view._first_render ||= self
view._last_render = self

view.send(:evaluate_assigns)
compile(local_assigns)
view.send(:set_controller_content_type, mime_type) if respond_to?(:mime_type)
view.send(:execute, method(local_assigns), local_assigns)
end

Expand Down
5 changes: 5 additions & 0 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -22,6 +22,11 @@ def format_and_extension
end
memoize :format_and_extension

def mime_type
Mime::Type.lookup_by_extension(format) if format
end
memoize :mime_type

def path
[base_path, [name, format, extension].compact.join('.')].compact.join('/')
end
Expand Down
3 changes: 1 addition & 2 deletions actionpack/lib/action_view/template_handlers/builder.rb
Expand Up @@ -6,8 +6,7 @@ class Builder < TemplateHandler
include Compilable

def compile(template)
# ActionMailer does not have a response
"controller.respond_to?(:response) && controller.response.content_type ||= Mime::XML;" +
"set_controller_content_type(Mime::XML);" +
"xml = ::Builder::XmlMarkup.new(:indent => 2);" +
"self.output_buffer = xml.target!;" +
template.source +
Expand Down
15 changes: 10 additions & 5 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -197,11 +197,11 @@ def default_render

def render_alternate_default
# For this test, the method "default_render" is overridden:
@alternate_default_render = lambda {
render :update do |page|
page.replace :foo, :partial => 'partial'
end
}
@alternate_default_render = lambda do
render :update do |page|
page.replace :foo, :partial => 'partial'
end
end
end

def rescue_action(e) raise end
Expand Down Expand Up @@ -467,6 +467,11 @@ def test_should_render_xml_but_keep_custom_content_type
get :render_xml_with_custom_content_type
assert_equal "application/atomsvc+xml", @response.content_type
end

def test_should_use_implicit_content_type
get :implicit_content_type, :format => 'atom'
assert_equal Mime::ATOM, @response.content_type
end
end

class EtagRenderTest < Test::Unit::TestCase
Expand Down
@@ -0,0 +1,2 @@
xml.atom do
end

2 comments on commit c3d1fda

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you used :memoize?
but :memoize “why would you ever want to use that?”

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Joshua Peek is a god!)

Please sign in to comment.