mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Remove some response content type concepts from ActionView
This commit is contained in:
@@ -122,12 +122,11 @@ module ActionController #:nodoc:
|
|||||||
# TODO: Remove this when new base is merged in
|
# TODO: Remove this when new base is merged in
|
||||||
if defined?(Http)
|
if defined?(Http)
|
||||||
@controller.formats = [mime_type.to_sym]
|
@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
|
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)
|
block_given? ? block.call : @controller.send(:render, :action => @controller.action_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ module ActionController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_body(action = nil, options = {})
|
def _normalize_options(action = nil, options = {})
|
||||||
if action.is_a?(Hash)
|
if action.is_a?(Hash)
|
||||||
options, action = action, nil
|
options, action = action, nil
|
||||||
elsif action.is_a?(String) || action.is_a?(Symbol)
|
elsif action.is_a?(String) || action.is_a?(Symbol)
|
||||||
@@ -87,9 +87,21 @@ module ActionController
|
|||||||
if options.key?(:action) && options[:action].to_s.index("/")
|
if options.key?(:action) && options[:action].to_s.index("/")
|
||||||
options[:template] = options.delete(:action)
|
options[:template] = options.delete(:action)
|
||||||
end
|
end
|
||||||
|
options
|
||||||
# options = {:template => options.to_s} if options.is_a?(String) || options.is_a?(Symbol)
|
end
|
||||||
super(options) || " "
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
# Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
|
# Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
|
||||||
|
|||||||
@@ -14,6 +14,16 @@ module ActionController
|
|||||||
super
|
super
|
||||||
end
|
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)
|
def render_to_body(options)
|
||||||
_process_options(options)
|
_process_options(options)
|
||||||
|
|
||||||
@@ -35,15 +45,7 @@ module ActionController
|
|||||||
options[:_prefix] = _prefix
|
options[:_prefix] = _prefix
|
||||||
end
|
end
|
||||||
|
|
||||||
ret = super(options)
|
super
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ module ActionController
|
|||||||
@request.recycle!
|
@request.recycle!
|
||||||
@response.recycle!
|
@response.recycle!
|
||||||
@controller.response_body = nil
|
@controller.response_body = nil
|
||||||
|
@controller.formats = nil
|
||||||
|
|
||||||
@html_document = nil
|
@html_document = nil
|
||||||
@request.env['REQUEST_METHOD'] = http_method
|
@request.env['REQUEST_METHOD'] = http_method
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|||||||
|
|
||||||
module Mime
|
module Mime
|
||||||
SET = []
|
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? }
|
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
|
||||||
|
|
||||||
def self.[](type)
|
def self.[](type)
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ module ActionDispatch
|
|||||||
if ActionController::Base.use_accept_header
|
if ActionController::Base.use_accept_header
|
||||||
Array(Mime[parameters[:format]] || accepts)
|
Array(Mime[parameters[:format]] || accepts)
|
||||||
else
|
else
|
||||||
[format]
|
[format, Mime[:all]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -288,8 +288,11 @@ module ActionView #:nodoc:
|
|||||||
end
|
end
|
||||||
|
|
||||||
def _set_controller_content_type(content_type) #:nodoc:
|
def _set_controller_content_type(content_type) #:nodoc:
|
||||||
if controller.respond_to?(:response)
|
# TODO: Remove this method when new base is switched
|
||||||
controller.response.content_type ||= content_type
|
unless defined?(ActionController::Http)
|
||||||
|
if controller.respond_to?(:response)
|
||||||
|
controller.response.content_type ||= content_type
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
require "active_support/core_ext/class/inheritable_attributes"
|
||||||
|
|
||||||
# Legacy TemplateHandler stub
|
# Legacy TemplateHandler stub
|
||||||
module ActionView
|
module ActionView
|
||||||
module TemplateHandlers #:nodoc:
|
module TemplateHandlers #:nodoc:
|
||||||
@@ -19,6 +21,9 @@ module ActionView
|
|||||||
end
|
end
|
||||||
|
|
||||||
class TemplateHandler
|
class TemplateHandler
|
||||||
|
extlib_inheritable_accessor :default_format
|
||||||
|
self.default_format = Mime::HTML
|
||||||
|
|
||||||
def self.call(template)
|
def self.call(template)
|
||||||
"#{name}.new(self).render(template, local_assigns)"
|
"#{name}.new(self).render(template, local_assigns)"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ module ActionView
|
|||||||
class Builder < TemplateHandler
|
class Builder < TemplateHandler
|
||||||
include Compilable
|
include Compilable
|
||||||
|
|
||||||
|
self.default_format = Mime::XML
|
||||||
|
|
||||||
def compile(template)
|
def compile(template)
|
||||||
"_set_controller_content_type(Mime::XML);" +
|
"_set_controller_content_type(Mime::XML);" +
|
||||||
"xml = ::Builder::XmlMarkup.new(:indent => 2);" +
|
"xml = ::Builder::XmlMarkup.new(:indent => 2);" +
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ module ActionView
|
|||||||
cattr_accessor :erb_trim_mode
|
cattr_accessor :erb_trim_mode
|
||||||
self.erb_trim_mode = '-'
|
self.erb_trim_mode = '-'
|
||||||
|
|
||||||
|
self.default_format = Mime::HTML
|
||||||
|
|
||||||
def compile(template)
|
def compile(template)
|
||||||
src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
|
src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,17 @@ module ActionView
|
|||||||
class RJS < TemplateHandler
|
class RJS < TemplateHandler
|
||||||
include Compilable
|
include Compilable
|
||||||
|
|
||||||
|
self.default_format = Mime::JS
|
||||||
|
|
||||||
def compile(template)
|
def compile(template)
|
||||||
"@formats = [:html];" +
|
"@formats = [:html];" +
|
||||||
"controller.response.content_type ||= Mime::JS;" +
|
"controller.response.content_type ||= Mime::JS;" +
|
||||||
"update_page do |page|;#{template.source}\nend"
|
"update_page do |page|;#{template.source}\nend"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def default_format
|
||||||
|
Mime::JS
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,13 +7,19 @@ require "action_view/template/path"
|
|||||||
module ActionView
|
module ActionView
|
||||||
class Template
|
class Template
|
||||||
extend TemplateHandlers
|
extend TemplateHandlers
|
||||||
attr_reader :source, :identifier, :handler
|
attr_reader :source, :identifier, :handler, :mime_type
|
||||||
|
|
||||||
def initialize(source, identifier, handler, details)
|
def initialize(source, identifier, handler, details)
|
||||||
@source = source
|
@source = source
|
||||||
@identifier = identifier
|
@identifier = identifier
|
||||||
@handler = handler
|
@handler = handler
|
||||||
@details = details
|
@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
|
end
|
||||||
|
|
||||||
def render(view, locals, &blk)
|
def render(view, locals, &blk)
|
||||||
@@ -35,12 +41,7 @@ module ActionView
|
|||||||
def partial?
|
def partial?
|
||||||
@details[:partial]
|
@details[:partial]
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Move out of Template
|
|
||||||
def mime_type
|
|
||||||
Mime::Type.lookup_by_extension(@details[:format].to_s) if @details[:format]
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def compile(locals, view)
|
def compile(locals, view)
|
||||||
|
|||||||
@@ -148,12 +148,13 @@ class AcceptBasedContentTypeTest < ActionController::TestCase
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
@_old_accept_header = ActionController::Base.use_accept_header
|
||||||
ActionController::Base.use_accept_header = true
|
ActionController::Base.use_accept_header = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
super
|
super
|
||||||
ActionController::Base.use_accept_header = false
|
ActionController::Base.use_accept_header = @_old_accept_header
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user