diff --git a/actionpack/lib/action_controller/base/mime_responds.rb b/actionpack/lib/action_controller/base/mime_responds.rb index e560376e0d..3c17dda1a1 100644 --- a/actionpack/lib/action_controller/base/mime_responds.rb +++ b/actionpack/lib/action_controller/base/mime_responds.rb @@ -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 diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 142699326e..b8674d5099 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -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: diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 81685ca9d6..840168397d 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -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 diff --git a/actionpack/lib/action_controller/testing/process2.rb b/actionpack/lib/action_controller/testing/process2.rb index 6a95e638cd..bee82c280e 100644 --- a/actionpack/lib/action_controller/testing/process2.rb +++ b/actionpack/lib/action_controller/testing/process2.rb @@ -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 diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index dfcf3a558f..25156a4c75 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -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) diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 13ff049a97..4831b89bde 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -175,7 +175,7 @@ module ActionDispatch if ActionController::Base.use_accept_header Array(Mime[parameters[:format]] || accepts) else - [format] + [format, Mime[:all]] end end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 56f0b5ef4f..6b72d406af 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -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 diff --git a/actionpack/lib/action_view/template/handler.rb b/actionpack/lib/action_view/template/handler.rb index 672da0ed2b..3481a4a4e7 100644 --- a/actionpack/lib/action_view/template/handler.rb +++ b/actionpack/lib/action_view/template/handler.rb @@ -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 diff --git a/actionpack/lib/action_view/template/handlers/builder.rb b/actionpack/lib/action_view/template/handlers/builder.rb index 788dc93326..f412228752 100644 --- a/actionpack/lib/action_view/template/handlers/builder.rb +++ b/actionpack/lib/action_view/template/handlers/builder.rb @@ -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);" + diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index fdcb108ffc..95f11d6490 100644 --- a/actionpack/lib/action_view/template/handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -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 diff --git a/actionpack/lib/action_view/template/handlers/rjs.rb b/actionpack/lib/action_view/template/handlers/rjs.rb index 802a79b3fc..a36744c2b7 100644 --- a/actionpack/lib/action_view/template/handlers/rjs.rb +++ b/actionpack/lib/action_view/template/handlers/rjs.rb @@ -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 diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index dcc5006103..0eedc596d2 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -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) diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 64b8b10d5b..d622ac1e85 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -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