Skip to content

Commit

Permalink
Merge pull request #12006 from kassio/11605-render-with-context-format
Browse files Browse the repository at this point in the history
Render with context format
  • Loading branch information
rafaelfranca committed Aug 26, 2013
2 parents adf6e30 + 424a5a7 commit e0fc5da
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 31 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,10 @@
## unreleased ##

* Fix to render partial by context(#11605).

*Kassio Borges*


* Fix `ActionDispatch::Assertions::ResponseAssertions#assert_redirected_to`
does not show user-supplied message.

Expand Down
7 changes: 2 additions & 5 deletions actionpack/lib/action_view/lookup_context.rb
Expand Up @@ -103,7 +103,7 @@ def _set_detail(key, value)

# Helpers related to template lookup using the lookup context information.
module ViewPaths
attr_reader :view_paths, :html_fallback_for_js
attr_reader :view_paths

# Whenever setting view paths, makes a copy so we can manipulate then in
# instance objects as we wish.
Expand Down Expand Up @@ -200,10 +200,7 @@ def initialize(view_paths, details = {}, prefixes = [])
def formats=(values)
if values
values.concat(default_formats) if values.delete "*/*"
if values == [:js]
values << :html
@html_fallback_for_js = true
end
values << :html if values == [:js]
end
super(values)
end
Expand Down
6 changes: 0 additions & 6 deletions actionpack/lib/action_view/renderer/abstract_renderer.rb
Expand Up @@ -37,11 +37,5 @@ def formats_regexp
def instrument(name, options={})
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
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
2 changes: 0 additions & 2 deletions actionpack/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -281,8 +281,6 @@ def setup(context, options, block)
@block = block
@details = extract_details(options)

prepend_formats(options[:formats])

if String === partial
@object = options[:object]
@path = partial
Expand Down
5 changes: 2 additions & 3 deletions actionpack/lib/action_view/renderer/template_renderer.rb
Expand Up @@ -10,10 +10,9 @@ def render(context, options)
template = determine_template(options)
context = @lookup_context

prepend_formats(template.formats)

unless context.rendered_format
context.rendered_format = template.formats.first || formats.last
context.formats = template.formats unless template.formats.empty?
context.rendered_format = context.formats.first
end

render_template(template, options[:layout], options[:locals])
Expand Down
1 change: 0 additions & 1 deletion actionpack/test/fixtures/test/_changing_priority.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion actionpack/test/fixtures/test/_changing_priority.json.erb

This file was deleted.

1 change: 0 additions & 1 deletion actionpack/test/fixtures/test/_first_json_partial.json.erb

This file was deleted.

Empty file.

This file was deleted.

1 change: 0 additions & 1 deletion actionpack/test/fixtures/test/html_template.html.erb

This file was deleted.

17 changes: 7 additions & 10 deletions actionpack/test/template/render_test.rb
Expand Up @@ -54,23 +54,20 @@ def test_render_partial_implicitly_use_format_of_the_rendered_template
assert_equal "Hello world", @view.render(:template => "test/one", :formats => [:html])
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_priority")
end

def test_render_template_with_a_missing_partial_of_another_format
@view.lookup_context.formats = [:html]
assert_raise ActionView::Template::Error, "Missing partial /missing with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder]}" do
@view.render(:template => "with_format", :formats => [:json])
end
end

def test_render_using_context_format_as_default
@view.lookup_context.formats = [:html]
assert_equal "partial html", @view.render('test/partial')
assert_equal "partial js", @view.render(:partial => 'test/partial', :formats => :js)
assert_equal "partial html", @view.render('test/partial')
end

def test_render_file_with_locale
assert_equal "<h1>Kein Kommentar</h1>", @view.render(:file => "comments/empty", :locale => [:de])
assert_equal "<h1>Kein Kommentar</h1>", @view.render(:file => "comments/empty", :locale => :de)
Expand Down

0 comments on commit e0fc5da

Please sign in to comment.