提交 69e349f1 编写于 作者: Y Yehuda Katz

Initial work to merge several places with similar logic

上级 06195239
......@@ -109,16 +109,13 @@ def respond_to(*types, &block)
end
class Responder #:nodoc:
def initialize(controller)
@controller = controller
@request = controller.request
@response = controller.response
if ActionController::Base.use_accept_header
@mime_type_priority = Array(Mime::Type.lookup_by_extension(@request.parameters[:format]) || @request.accepts)
else
@mime_type_priority = [@request.format]
end
@mime_type_priority = @request.formats
@order = []
@responses = {}
......
......@@ -5,6 +5,10 @@ module Mime
EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
def self.[](type)
Type.lookup_by_extension(type)
end
# Encapsulates the notion of a mime type. Can be used at render time, for example, with:
#
# class PostsController < ActionController::Base
......
......@@ -152,24 +152,33 @@ def fresh?(response)
end
end
ONLY_ALL = [Mime::ALL].freeze
# Returns the Mime type for the \format used in the request.
#
# GET /posts/5.xml | request.format => Mime::XML
# GET /posts/5.xhtml | request.format => Mime::HTML
# GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt>
def format
@format ||=
if parameters[:format]
Mime::Type.lookup_by_extension(parameters[:format])
elsif ActionController::Base.use_accept_header
accepts.first
elsif xhr?
Mime::Type.lookup_by_extension("js")
else
Mime::Type.lookup_by_extension("html")
Mime[parameters[:format]]
elsif Base.use_accept_header && !(accepts == ONLY_ALL)
accepts.first
elsif xhr? then Mime::JS
else Mime::HTML
end
end
def formats
@formats =
if Base.use_accept_header
Array(Mime[parameters[:format]] || accepts)
else
[format]
end
end
# Sets the \format by string extension, which can be used to force custom formats
# that are not controlled by the extension.
......
......@@ -266,7 +266,7 @@ def template_format
if defined? @template_format
@template_format
elsif controller && controller.respond_to?(:request)
@template_format = controller.request.template_format.to_sym
@template_format = controller.request.format.to_sym
else
@template_format = :html
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册