From c3d1fda555c4bd5f8821d830c685ae5d0e7e52d0 Mon Sep 17 00:00:00 2001 From: Tom Ward Date: Fri, 18 Jul 2008 20:14:12 -0500 Subject: Set the response content type to that of found template if not explicitly set elsewhere [#444 state:resolved] Signed-off-by: Joshua Peek --- actionpack/lib/action_view/base.rb | 6 ++++++ actionpack/lib/action_view/renderable.rb | 5 ++++- actionpack/lib/action_view/template.rb | 5 +++++ actionpack/lib/action_view/template_handlers/builder.rb | 3 +-- 4 files changed, 16 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index ae6b284854..fe51af62e6 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -379,6 +379,12 @@ module ActionView #:nodoc: @assigns.each { |key, value| instance_variable_set("@#{key}", value) } end + def set_controller_content_type(content_type) + if controller.respond_to?(:response) + controller.response.content_type ||= content_type + end + end + def execute(method, local_assigns = {}) send(method, local_assigns) do |*names| instance_variable_get "@content_for_#{names.first || 'layout'}" diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/renderable.rb index 4f865cbced..2b825ac4e9 100644 --- a/actionpack/lib/action_view/renderable.rb +++ b/actionpack/lib/action_view/renderable.rb @@ -24,10 +24,13 @@ module ActionView memoize :compiled_source def render(view, local_assigns = {}) + compile(local_assigns) + view._first_render ||= self view._last_render = self + view.send(:evaluate_assigns) - compile(local_assigns) + view.send(:set_controller_content_type, mime_type) if respond_to?(:mime_type) view.send(:execute, method(local_assigns), local_assigns) end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index eba42518d7..1f528dd900 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -22,6 +22,11 @@ module ActionView #:nodoc: end memoize :format_and_extension + def mime_type + Mime::Type.lookup_by_extension(format) if format + end + memoize :mime_type + def path [base_path, [name, format, extension].compact.join('.')].compact.join('/') end diff --git a/actionpack/lib/action_view/template_handlers/builder.rb b/actionpack/lib/action_view/template_handlers/builder.rb index 335ec1abb4..7d24a5c423 100644 --- a/actionpack/lib/action_view/template_handlers/builder.rb +++ b/actionpack/lib/action_view/template_handlers/builder.rb @@ -6,8 +6,7 @@ module ActionView include Compilable def compile(template) - # ActionMailer does not have a response - "controller.respond_to?(:response) && controller.response.content_type ||= Mime::XML;" + + "set_controller_content_type(Mime::XML);" + "xml = ::Builder::XmlMarkup.new(:indent => 2);" + "self.output_buffer = xml.target!;" + template.source + -- cgit v1.2.3