Permalink
Browse files

Move template_format logic out to the request so it's alongside the '…

…regular' request format.

Use xhr? instead of the expensive trip through Request#accepts.
  • Loading branch information...
1 parent dc2d754 commit 12cf8f348b591b7bb0dc899345293a8e37ddad7c @NZKoz NZKoz committed Jun 27, 2008
@@ -116,6 +116,19 @@ def format=(extension)
@format = Mime::Type.lookup_by_extension(parameters[:format])
end
+ def template_format
+ parameter_format = parameters[:format]
+
+ case
+ when parameter_format.blank? && !xhr?
+ :html
+ when parameter_format.blank? && xhr?
+ :js
+ else
+ parameter_format.to_sym
+ end
+ end
+
# Returns true if the request's "X-Requested-With" header contains
# "XMLHttpRequest". (The Prototype Javascript library sends this header with
# every Ajax request.)
@@ -273,17 +273,7 @@ def template_format
return @template_format if @template_format
if controller && controller.respond_to?(:request)
- parameter_format = controller.request.parameters[:format]
- accept_format = controller.request.accepts.first
-
- case
- when parameter_format.blank? && accept_format != :js
- @template_format = :html
- when parameter_format.blank? && accept_format == :js
- @template_format = :js
- else
- @template_format = parameter_format.to_sym
- end
+ @template_format = controller.request.template_format
else
@template_format = :html
end
@@ -605,8 +605,7 @@ def test_render_xml_with_default
end
def test_render_with_default_from_accept_header
- @request.env["HTTP_ACCEPT"] = "text/javascript"
- get :greeting
+ xhr :get, :greeting
assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
end

0 comments on commit 12cf8f3

Please sign in to comment.