Remove some response content type concepts from ActionView

上级 386ff66e
......@@ -122,12 +122,11 @@ def custom(mime_type, &block)
# TODO: Remove this when new base is merged in
if defined?(Http)
@controller.formats = [mime_type.to_sym]
@controller.template.formats = [mime_type.to_sym]
else
@controller.template.formats = [mime_type.to_sym]
@response.content_type = mime_type.to_s
end
@controller.template.formats = [mime_type.to_sym]
@response.content_type = mime_type.to_s
block_given? ? block.call : @controller.send(:render, :action => @controller.action_name)
end
end
......
......@@ -70,7 +70,7 @@ def self.app_loaded!
end
end
def render_to_body(action = nil, options = {})
def _normalize_options(action = nil, options = {})
if action.is_a?(Hash)
options, action = action, nil
elsif action.is_a?(String) || action.is_a?(Symbol)
......@@ -87,9 +87,21 @@ def render_to_body(action = nil, options = {})
if options.key?(:action) && options[:action].to_s.index("/")
options[:template] = options.delete(:action)
end
# options = {:template => options.to_s} if options.is_a?(String) || options.is_a?(Symbol)
super(options) || " "
options
end
def render(action = nil, options = {})
options = _normalize_options(action, options)
super(options)
end
def render_to_string(action = nil, options = {})
options = _normalize_options(action, options)
super(options)
end
def render_to_body(options)
super || [" "]
end
# Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
......
......@@ -14,6 +14,16 @@ def response_body=(body)
super
end
def render(options)
super
options[:_template] ||= _action_view._partial
response.content_type ||= begin
mime = options[:_template].mime_type
formats.include?(mime && mime.to_sym) || formats.include?(:all) ? mime : Mime::Type.lookup_by_extension(formats.first)
end
response_body
end
def render_to_body(options)
_process_options(options)
......@@ -35,15 +45,7 @@ def render_to_body(options)
options[:_prefix] = _prefix
end
ret = super(options)
options[:_template] ||= _action_view._partial
response.content_type ||= begin
mime = options[:_template].mime_type
mime &&= mime.to_sym
formats.include?(mime) ? mime : formats.first
end
ret
super
end
private
......
......@@ -40,6 +40,7 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
@request.recycle!
@response.recycle!
@controller.response_body = nil
@controller.formats = nil
@html_document = nil
@request.env['REQUEST_METHOD'] = http_method
......
......@@ -3,7 +3,7 @@
module Mime
SET = []
EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
EXTENSION_LOOKUP = {}
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
def self.[](type)
......
......@@ -175,7 +175,7 @@ def formats
if ActionController::Base.use_accept_header
Array(Mime[parameters[:format]] || accepts)
else
[format]
[format, Mime[:all]]
end
end
......
......@@ -288,8 +288,11 @@ def _copy_ivars_from_controller #:nodoc:
end
def _set_controller_content_type(content_type) #:nodoc:
if controller.respond_to?(:response)
controller.response.content_type ||= content_type
# TODO: Remove this method when new base is switched
unless defined?(ActionController::Http)
if controller.respond_to?(:response)
controller.response.content_type ||= content_type
end
end
end
end
......
require "active_support/core_ext/class/inheritable_attributes"
# Legacy TemplateHandler stub
module ActionView
module TemplateHandlers #:nodoc:
......@@ -19,6 +21,9 @@ def compile(template)
end
class TemplateHandler
extlib_inheritable_accessor :default_format
self.default_format = Mime::HTML
def self.call(template)
"#{name}.new(self).render(template, local_assigns)"
end
......
......@@ -5,6 +5,8 @@ module TemplateHandlers
class Builder < TemplateHandler
include Compilable
self.default_format = Mime::XML
def compile(template)
"_set_controller_content_type(Mime::XML);" +
"xml = ::Builder::XmlMarkup.new(:indent => 2);" +
......
......@@ -13,6 +13,8 @@ class ERB < TemplateHandler
cattr_accessor :erb_trim_mode
self.erb_trim_mode = '-'
self.default_format = Mime::HTML
def compile(template)
src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
......
......@@ -3,11 +3,17 @@ module TemplateHandlers
class RJS < TemplateHandler
include Compilable
self.default_format = Mime::JS
def compile(template)
"@formats = [:html];" +
"controller.response.content_type ||= Mime::JS;" +
"update_page do |page|;#{template.source}\nend"
end
def default_format
Mime::JS
end
end
end
end
......@@ -7,13 +7,19 @@
module ActionView
class Template
extend TemplateHandlers
attr_reader :source, :identifier, :handler
attr_reader :source, :identifier, :handler, :mime_type
def initialize(source, identifier, handler, details)
@source = source
@identifier = identifier
@handler = handler
@details = details
format = details[:format] || begin
# TODO: Clean this up
handler.respond_to?(:default_format) ? handler.default_format.to_sym.to_s : "html"
end
@mime_type = Mime::Type.lookup_by_extension(format.to_s)
end
def render(view, locals, &blk)
......@@ -35,12 +41,7 @@ def counter_name
def partial?
@details[:partial]
end
# TODO: Move out of Template
def mime_type
Mime::Type.lookup_by_extension(@details[:format].to_s) if @details[:format]
end
private
def compile(locals, view)
......
......@@ -148,12 +148,13 @@ class AcceptBasedContentTypeTest < ActionController::TestCase
def setup
super
@_old_accept_header = ActionController::Base.use_accept_header
ActionController::Base.use_accept_header = true
end
def teardown
super
ActionController::Base.use_accept_header = false
ActionController::Base.use_accept_header = @_old_accept_header
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册