mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Common behavior with adding formats to lookup_context for TemplateRenderer and PartialRenderer
This commit is contained in:
parent
060fca7897
commit
2c22376fe0
12 changed files with 33 additions and 4 deletions
|
@ -103,7 +103,7 @@ module ActionView
|
||||||
|
|
||||||
# Helpers related to template lookup using the lookup context information.
|
# Helpers related to template lookup using the lookup context information.
|
||||||
module ViewPaths
|
module ViewPaths
|
||||||
attr_reader :view_paths
|
attr_reader :view_paths, :html_fallback_for_js
|
||||||
|
|
||||||
# Whenever setting view paths, makes a copy so we can manipulate then in
|
# Whenever setting view paths, makes a copy so we can manipulate then in
|
||||||
# instance objects as we wish.
|
# instance objects as we wish.
|
||||||
|
@ -200,7 +200,10 @@ module ActionView
|
||||||
def formats=(values)
|
def formats=(values)
|
||||||
if values
|
if values
|
||||||
values.concat(default_formats) if values.delete "*/*"
|
values.concat(default_formats) if values.delete "*/*"
|
||||||
values << :html if values == [:js]
|
if values == [:js]
|
||||||
|
values << :html
|
||||||
|
@html_fallback_for_js = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
super(values)
|
super(values)
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,5 +37,11 @@ module ActionView
|
||||||
def instrument(name, options={})
|
def instrument(name, options={})
|
||||||
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
|
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prepend_formats(formats)
|
||||||
|
formats = Array(formats)
|
||||||
|
return if formats.empty? || @lookup_context.html_fallback_for_js
|
||||||
|
@lookup_context.formats = formats | @lookup_context.formats
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -281,6 +281,8 @@ module ActionView
|
||||||
@block = block
|
@block = block
|
||||||
@details = extract_details(options)
|
@details = extract_details(options)
|
||||||
|
|
||||||
|
prepend_formats(options[:formats])
|
||||||
|
|
||||||
if String === partial
|
if String === partial
|
||||||
@object = options[:object]
|
@object = options[:object]
|
||||||
@path = partial
|
@path = partial
|
||||||
|
|
|
@ -10,9 +10,10 @@ module ActionView
|
||||||
template = determine_template(options)
|
template = determine_template(options)
|
||||||
context = @lookup_context
|
context = @lookup_context
|
||||||
|
|
||||||
|
prepend_formats(template.formats)
|
||||||
|
|
||||||
unless context.rendered_format
|
unless context.rendered_format
|
||||||
context.formats = template.formats unless template.formats.empty?
|
context.rendered_format = template.formats.first || formats.last
|
||||||
context.rendered_format = context.formats.first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
render_template(template, options[:layout], options[:locals])
|
render_template(template, options[:layout], options[:locals])
|
||||||
|
|
1
actionpack/test/fixtures/test/_changing_priority.html.erb
vendored
Normal file
1
actionpack/test/fixtures/test/_changing_priority.html.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
HTML
|
1
actionpack/test/fixtures/test/_changing_priority.json.erb
vendored
Normal file
1
actionpack/test/fixtures/test/_changing_priority.json.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
JSON
|
1
actionpack/test/fixtures/test/_first_json_partial.json.erb
vendored
Normal file
1
actionpack/test/fixtures/test/_first_json_partial.json.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<%= render :partial => "test/second_json_partial" %>
|
0
actionpack/test/fixtures/test/_json_change_priority.json.erb
vendored
Normal file
0
actionpack/test/fixtures/test/_json_change_priority.json.erb
vendored
Normal file
1
actionpack/test/fixtures/test/_second_json_partial.json.erb
vendored
Normal file
1
actionpack/test/fixtures/test/_second_json_partial.json.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Third level
|
2
actionpack/test/fixtures/test/change_priorty.html.erb
vendored
Normal file
2
actionpack/test/fixtures/test/change_priorty.html.erb
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
<%= render :partial => "test/json_change_priority", formats: :json %>
|
||||||
|
HTML Template, but <%= render :partial => "test/changing_priority" %> partial
|
1
actionpack/test/fixtures/test/html_template.html.erb
vendored
Normal file
1
actionpack/test/fixtures/test/html_template.html.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<%= render :partial => "test/first_json_partial", formats: :json %>
|
|
@ -54,6 +54,16 @@ module RenderTestCases
|
||||||
assert_equal "Hello world", @view.render(:template => "test/one", :formats => [:html])
|
assert_equal "Hello world", @view.render(:template => "test/one", :formats => [:html])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_partial_implicitly_use_format_of_the_rendered_partial
|
||||||
|
@view.lookup_context.formats = [:html]
|
||||||
|
assert_equal "Third level", @view.render(:template => "test/html_template")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_partial_use_last_prepended_format_for_partials_with_the_same_names
|
||||||
|
@view.lookup_context.formats = [:html]
|
||||||
|
assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priorty")
|
||||||
|
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.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
|
||||||
|
|
Loading…
Reference in a new issue