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

Remove some response content type concepts from ActionView

This commit is contained in:
Yehuda Katz + Carl Lerche 2009-05-21 14:22:07 -07:00
parent 386ff66e5e
commit e693f45e15
13 changed files with 63 additions and 29 deletions

View file

@ -122,12 +122,11 @@ module ActionController #:nodoc:
# 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

View file

@ -70,7 +70,7 @@ module ActionController
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 @@ module ActionController
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:

View file

@ -14,6 +14,16 @@ module ActionController
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 @@ module ActionController
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

View file

@ -40,6 +40,7 @@ module ActionController
@request.recycle!
@response.recycle!
@controller.response_body = nil
@controller.formats = nil
@html_document = nil
@request.env['REQUEST_METHOD'] = http_method

View file

@ -3,7 +3,7 @@ require 'active_support/core_ext/class/attribute_accessors'
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)

View file

@ -175,7 +175,7 @@ module ActionDispatch
if ActionController::Base.use_accept_header
Array(Mime[parameters[:format]] || accepts)
else
[format]
[format, Mime[:all]]
end
end

View file

@ -288,8 +288,11 @@ module ActionView #: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

View file

@ -1,3 +1,5 @@
require "active_support/core_ext/class/inheritable_attributes"
# Legacy TemplateHandler stub
module ActionView
module TemplateHandlers #:nodoc:
@ -19,6 +21,9 @@ module ActionView
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

View file

@ -5,6 +5,8 @@ module ActionView
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);" +

View file

@ -13,6 +13,8 @@ module ActionView
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

View file

@ -3,11 +3,17 @@ module ActionView
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

View file

@ -7,13 +7,19 @@ require "action_view/template/path"
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 @@ module ActionView
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)

View file

@ -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