Skip to content

Commit 157ea76

Browse files
committed
format lookup for partials is derived from the format in which the template is being rendered
Closes rails#5025 part 2
1 parent a95f730 commit 157ea76

File tree

11 files changed

+25
-36
lines changed

11 files changed

+25
-36
lines changed

actionmailer/lib/action_mailer/collector.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def any(*args, &block)
2222

2323
def custom(mime, options={})
2424
options.reverse_merge!(:content_type => mime.to_s)
25-
@context.freeze_formats([mime.to_sym])
25+
@context.formats = [mime.to_sym]
2626
options[:body] = block_given? ? yield : @default_render.call
2727
@responses << options
2828
end
2929
end
30-
end
30+
end

actionpack/lib/action_controller/metal/mime_responds.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
280280

281281
if format
282282
self.content_type ||= format.to_s
283-
lookup_context.freeze_formats([format.to_sym])
283+
lookup_context.formats = [format.to_sym]
284284
collector
285285
else
286286
head :not_acceptable

actionpack/lib/action_controller/metal/rendering.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def process_action(*) #:nodoc:
1414
def render(*args) #:nodoc:
1515
raise ::AbstractController::DoubleRenderError if response_body
1616
super
17-
self.content_type ||= Mime[formats.first].to_s
17+
self.content_type ||= Mime[lookup_context.rendered_format].to_s
1818
response_body
1919
end
2020

actionpack/lib/action_view/lookup_context.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ActionView
99
# generate a key, given to view paths, used in the resolver cache lookup. Since
1010
# this key is generated just once during the request, it speeds up all cache accesses.
1111
class LookupContext #:nodoc:
12-
attr_accessor :prefixes
12+
attr_accessor :prefixes, :rendered_format
1313

1414
mattr_accessor :fallbacks
1515
@@fallbacks = FallbackFileSystemResolver.instances
@@ -170,23 +170,15 @@ def normalize_name(name, prefixes) #:nodoc:
170170

171171
def initialize(view_paths, details = {}, prefixes = [])
172172
@details, @details_key = {}, nil
173-
@frozen_formats, @skip_default_locale = false, false
173+
@skip_default_locale = false
174174
@cache = true
175175
@prefixes = prefixes
176+
@rendered_format = nil
176177

177178
self.view_paths = view_paths
178179
initialize_details(details)
179180
end
180181

181-
# Freeze the current formats in the lookup context. By freezing them, you are guaranteeing
182-
# that next template lookups are not going to modify the formats. The controller can also
183-
# use this, to ensure that formats won't be further modified (as it does in respond_to blocks).
184-
def freeze_formats(formats, unless_frozen=false) #:nodoc:
185-
return if unless_frozen && @frozen_formats
186-
self.formats = formats
187-
@frozen_formats = true
188-
end
189-
190182
# Override formats= to expand ["*/*"] values and automatically
191183
# add :html as fallback to :js.
192184
def formats=(values)

actionpack/lib/action_view/renderer/abstract_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ActionView
22
class AbstractRenderer #:nodoc:
33
delegate :find_template, :template_exists?, :with_fallbacks, :update_details,
4-
:with_layout_format, :formats, :freeze_formats, :to => :@lookup_context
4+
:with_layout_format, :formats, :to => :@lookup_context
55

66
def initialize(lookup_context)
77
@lookup_context = lookup_context

actionpack/lib/action_view/renderer/template_renderer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ def render(context, options)
66
@view = context
77
@details = extract_details(options)
88
template = determine_template(options)
9-
freeze_formats(template.formats, true)
9+
@lookup_context.rendered_format ||= template.formats.first
10+
@lookup_context.formats = template.formats
1011
render_template(template, options[:layout], options[:locals])
1112
end
1213

actionpack/lib/action_view/template.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,12 @@ def mime_type
159159
# virtual path set (true just for inline templates).
160160
def refresh(view)
161161
raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path
162-
begin
163-
lookup = view.lookup_context
164-
pieces = @virtual_path.split("/")
165-
name = pieces.pop
166-
partial = !!name.sub!(/^_/, "")
167-
previous_formats, lookup.formats = lookup.formats, @formats
168-
lookup.disable_cache do
169-
lookup.find_template(name, [ pieces.join('/') ], partial, @locals)
170-
end
171-
ensure
172-
lookup.formats = previous_formats
162+
lookup = view.lookup_context
163+
pieces = @virtual_path.split("/")
164+
name = pieces.pop
165+
partial = !!name.sub!(/^_/, "")
166+
lookup.disable_cache do
167+
lookup.find_template(name, [ pieces.join('/') ], partial, @locals)
173168
end
174169
end
175170

actionpack/lib/action_view/template/handlers/erb.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ class ERB
4444
class_attribute :erb_trim_mode
4545
self.erb_trim_mode = '-'
4646

47-
# Default format used by ERB.
48-
class_attribute :default_format
49-
self.default_format = Mime::HTML
50-
5147
# Default implementation used.
5248
class_attribute :erb_implementation
5349
self.erb_implementation = Erubis
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render :partial => "test/two" %> world

actionpack/test/template/lookup_context_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def teardown
7878
end
7979

8080
test "found templates respects given formats if one cannot be found from template or handler" do
81-
ActionView::Template::Handlers::ERB.expects(:default_format).returns(nil)
81+
ActionView::Template::Handlers::Builder.expects(:default_format).returns(nil)
8282
@lookup_context.formats = [:text]
83-
template = @lookup_context.find("hello_world", %w(test))
83+
template = @lookup_context.find("hello", %w(test))
8484
assert_equal [:text], template.formats
8585
end
8686

0 commit comments

Comments
 (0)