public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Set the response content type to that of found template if not explicitly set 
elsewhere [#444 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
tomafro (author)
Fri Jul 18 18:14:12 -0700 2008
josh (committer)
Fri Jul 18 18:14:12 -0700 2008
commit  c3d1fda555c4bd5f8821d830c685ae5d0e7e52d0
tree    41a5e9b018d724a82c33ab117bc1e6403e6cbbc5
parent  d2ccb852d4e1f6f1b01e43f32213053ae3bef408
...
379
380
381
 
 
 
 
 
 
382
383
384
...
379
380
381
382
383
384
385
386
387
388
389
390
0
@@ -379,6 +379,12 @@ module ActionView #:nodoc:
0
         @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
0
       end
0
 
0
+      def set_controller_content_type(content_type)
0
+        if controller.respond_to?(:response)
0
+          controller.response.content_type ||= content_type
0
+        end
0
+      end
0
+
0
       def execute(method, local_assigns = {})
0
         send(method, local_assigns) do |*names|
0
           instance_variable_get "@content_for_#{names.first || 'layout'}"
...
24
25
26
 
 
27
28
 
29
30
 
31
32
33
...
24
25
26
27
28
29
30
31
32
 
33
34
35
36
0
@@ -24,10 +24,13 @@ module ActionView
0
     memoize :compiled_source
0
 
0
     def render(view, local_assigns = {})
0
+      compile(local_assigns)
0
+
0
       view._first_render ||= self
0
       view._last_render = self
0
+
0
       view.send(:evaluate_assigns)
0
-      compile(local_assigns)
0
+      view.send(:set_controller_content_type, mime_type) if respond_to?(:mime_type)
0
       view.send(:execute, method(local_assigns), local_assigns)
0
     end
0
 
...
22
23
24
 
 
 
 
 
25
26
27
...
22
23
24
25
26
27
28
29
30
31
32
0
@@ -22,6 +22,11 @@ module ActionView #:nodoc:
0
     end
0
     memoize :format_and_extension
0
 
0
+    def mime_type
0
+      Mime::Type.lookup_by_extension(format) if format
0
+    end
0
+    memoize :mime_type
0
+
0
     def path
0
       [base_path, [name, format, extension].compact.join('.')].compact.join('/')
0
     end
...
6
7
8
9
10
 
11
12
13
...
6
7
8
 
 
9
10
11
12
0
@@ -6,8 +6,7 @@ module ActionView
0
       include Compilable
0
 
0
       def compile(template)
0
-        # ActionMailer does not have a response
0
-        "controller.respond_to?(:response) && controller.response.content_type ||= Mime::XML;" +
0
+        "set_controller_content_type(Mime::XML);" +
0
           "xml = ::Builder::XmlMarkup.new(:indent => 2);" +
0
           "self.output_buffer = xml.target!;" +
0
           template.source +
...
197
198
199
200
201
202
203
204
 
 
 
 
 
205
206
207
...
467
468
469
 
 
 
 
 
470
471
472
...
197
198
199
 
 
 
 
 
200
201
202
203
204
205
206
207
...
467
468
469
470
471
472
473
474
475
476
477
0
@@ -197,11 +197,11 @@ class TestController < ActionController::Base
0
 
0
   def render_alternate_default
0
     # For this test, the method "default_render" is overridden:
0
-    @alternate_default_render = lambda {
0
-  render :update do |page|
0
-    page.replace :foo, :partial => 'partial'
0
-  end
0
-      }
0
+    @alternate_default_render = lambda do
0
+      render :update do |page|
0
+        page.replace :foo, :partial => 'partial'
0
+      end
0
+    end
0
   end
0
 
0
   def rescue_action(e) raise end
0
@@ -467,6 +467,11 @@ class RenderTest < Test::Unit::TestCase
0
     get :render_xml_with_custom_content_type
0
     assert_equal "application/atomsvc+xml", @response.content_type
0
   end
0
+
0
+  def test_should_use_implicit_content_type
0
+    get :implicit_content_type, :format => 'atom'
0
+    assert_equal Mime::ATOM, @response.content_type
0
+  end
0
 end
0
 
0
 class EtagRenderTest < Test::Unit::TestCase

Comments

matthewrudy Fri Jul 18 19:13:28 -0700 2008

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

matthewrudy Fri Jul 18 19:13:56 -0700 2008

(Joshua Peek is a god!)