1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Michael Koziarski 2008-06-27 21:24:21 +03:00
parent dc2d754d60
commit 12cf8f348b
3 changed files with 15 additions and 13 deletions

View file

@ -116,6 +116,19 @@ module ActionController
@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.)

View file

@ -273,17 +273,7 @@ module ActionView #:nodoc:
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

View file

@ -605,8 +605,7 @@ EOS
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