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

format lookup for partials is derived from the format in which the template is being rendered

Closes #5025 part 2
This commit is contained in:
Santiago Pastorino 2012-02-21 23:55:56 -02:00
parent a95f730ea4
commit 157ea76306
11 changed files with 25 additions and 36 deletions

View file

@ -22,9 +22,9 @@ module ActionMailer #:nodoc:
def custom(mime, options={}) def custom(mime, options={})
options.reverse_merge!(:content_type => mime.to_s) options.reverse_merge!(:content_type => mime.to_s)
@context.freeze_formats([mime.to_sym]) @context.formats = [mime.to_sym]
options[:body] = block_given? ? yield : @default_render.call options[:body] = block_given? ? yield : @default_render.call
@responses << options @responses << options
end end
end end
end end

View file

@ -280,7 +280,7 @@ module ActionController #:nodoc:
if format if format
self.content_type ||= format.to_s self.content_type ||= format.to_s
lookup_context.freeze_formats([format.to_sym]) lookup_context.formats = [format.to_sym]
collector collector
else else
head :not_acceptable head :not_acceptable

View file

@ -14,7 +14,7 @@ module ActionController
def render(*args) #:nodoc: def render(*args) #:nodoc:
raise ::AbstractController::DoubleRenderError if response_body raise ::AbstractController::DoubleRenderError if response_body
super super
self.content_type ||= Mime[formats.first].to_s self.content_type ||= Mime[lookup_context.rendered_format].to_s
response_body response_body
end end

View file

@ -9,7 +9,7 @@ module ActionView
# generate a key, given to view paths, used in the resolver cache lookup. Since # generate a key, given to view paths, used in the resolver cache lookup. Since
# this key is generated just once during the request, it speeds up all cache accesses. # this key is generated just once during the request, it speeds up all cache accesses.
class LookupContext #:nodoc: class LookupContext #:nodoc:
attr_accessor :prefixes attr_accessor :prefixes, :rendered_format
mattr_accessor :fallbacks mattr_accessor :fallbacks
@@fallbacks = FallbackFileSystemResolver.instances @@fallbacks = FallbackFileSystemResolver.instances
@ -170,23 +170,15 @@ module ActionView
def initialize(view_paths, details = {}, prefixes = []) def initialize(view_paths, details = {}, prefixes = [])
@details, @details_key = {}, nil @details, @details_key = {}, nil
@frozen_formats, @skip_default_locale = false, false @skip_default_locale = false
@cache = true @cache = true
@prefixes = prefixes @prefixes = prefixes
@rendered_format = nil
self.view_paths = view_paths self.view_paths = view_paths
initialize_details(details) initialize_details(details)
end end
# Freeze the current formats in the lookup context. By freezing them, you are guaranteeing
# that next template lookups are not going to modify the formats. The controller can also
# use this, to ensure that formats won't be further modified (as it does in respond_to blocks).
def freeze_formats(formats, unless_frozen=false) #:nodoc:
return if unless_frozen && @frozen_formats
self.formats = formats
@frozen_formats = true
end
# Override formats= to expand ["*/*"] values and automatically # Override formats= to expand ["*/*"] values and automatically
# add :html as fallback to :js. # add :html as fallback to :js.
def formats=(values) def formats=(values)

View file

@ -1,7 +1,7 @@
module ActionView module ActionView
class AbstractRenderer #:nodoc: class AbstractRenderer #:nodoc:
delegate :find_template, :template_exists?, :with_fallbacks, :update_details, delegate :find_template, :template_exists?, :with_fallbacks, :update_details,
:with_layout_format, :formats, :freeze_formats, :to => :@lookup_context :with_layout_format, :formats, :to => :@lookup_context
def initialize(lookup_context) def initialize(lookup_context)
@lookup_context = lookup_context @lookup_context = lookup_context

View file

@ -6,7 +6,8 @@ module ActionView
@view = context @view = context
@details = extract_details(options) @details = extract_details(options)
template = determine_template(options) template = determine_template(options)
freeze_formats(template.formats, true) @lookup_context.rendered_format ||= template.formats.first
@lookup_context.formats = template.formats
render_template(template, options[:layout], options[:locals]) render_template(template, options[:layout], options[:locals])
end end

View file

@ -159,17 +159,12 @@ module ActionView
# virtual path set (true just for inline templates). # virtual path set (true just for inline templates).
def refresh(view) def refresh(view)
raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path
begin lookup = view.lookup_context
lookup = view.lookup_context pieces = @virtual_path.split("/")
pieces = @virtual_path.split("/") name = pieces.pop
name = pieces.pop partial = !!name.sub!(/^_/, "")
partial = !!name.sub!(/^_/, "") lookup.disable_cache do
previous_formats, lookup.formats = lookup.formats, @formats lookup.find_template(name, [ pieces.join('/') ], partial, @locals)
lookup.disable_cache do
lookup.find_template(name, [ pieces.join('/') ], partial, @locals)
end
ensure
lookup.formats = previous_formats
end end
end end

View file

@ -44,10 +44,6 @@ module ActionView
class_attribute :erb_trim_mode class_attribute :erb_trim_mode
self.erb_trim_mode = '-' self.erb_trim_mode = '-'
# Default format used by ERB.
class_attribute :default_format
self.default_format = Mime::HTML
# Default implementation used. # Default implementation used.
class_attribute :erb_implementation class_attribute :erb_implementation
self.erb_implementation = Erubis self.erb_implementation = Erubis

View file

@ -0,0 +1 @@
<%= render :partial => "test/two" %> world

View file

@ -78,9 +78,9 @@ class LookupContextTest < ActiveSupport::TestCase
end end
test "found templates respects given formats if one cannot be found from template or handler" do test "found templates respects given formats if one cannot be found from template or handler" do
ActionView::Template::Handlers::ERB.expects(:default_format).returns(nil) ActionView::Template::Handlers::Builder.expects(:default_format).returns(nil)
@lookup_context.formats = [:text] @lookup_context.formats = [:text]
template = @lookup_context.find("hello_world", %w(test)) template = @lookup_context.find("hello", %w(test))
assert_equal [:text], template.formats assert_equal [:text], template.formats
end end

View file

@ -51,12 +51,16 @@ module RenderTestCases
assert_match "<error>No Comment</error>", @view.render(:template => "comments/empty", :formats => [:xml]) assert_match "<error>No Comment</error>", @view.render(:template => "comments/empty", :formats => [:xml])
end end
def test_render_partial_implicitly_use_format_of_the_rendered_template
@view.lookup_context.formats = [:json]
assert_equal "Hello world", @view.render(:template => "test/one", :formats => [:html])
end
def test_render_template_with_a_missing_partial_of_another_format def test_render_template_with_a_missing_partial_of_another_format
@view.lookup_context.freeze_formats([:html]) @view.lookup_context.formats = [:html]
assert_raise ActionView::Template::Error, "Missing partial /missing with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]}" do assert_raise ActionView::Template::Error, "Missing partial /missing with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]}" do
@view.render(:template => "with_format", :formats => [:json]) @view.render(:template => "with_format", :formats => [:json])
end end
assert_equal [:html], @view.lookup_context.formats
end end
def test_render_file_with_locale def test_render_file_with_locale