Skip to content

Commit

Permalink
recognizes when a partial was rendered twice. Closes #3675
Browse files Browse the repository at this point in the history
  • Loading branch information
senny authored and Yves Senn committed Oct 11, 2012
1 parent cd98c25 commit ed95674
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* `assert_template` can be used to assert on the same template with different locals
Fix #3675

*Yves Senn*

* Remove old asset tag concatenation (no longer needed now that we have the asset pipeline). *Josh Peek* * Remove old asset tag concatenation (no longer needed now that we have the asset pipeline). *Josh Peek*


* Accept :remote as symbolic option for `link_to` helper. *Riley Lynch* * Accept :remote as symbolic option for `link_to` helper. *Riley Lynch*
Expand Down
14 changes: 10 additions & 4 deletions actionpack/lib/action_controller/test_case.rb
Expand Up @@ -123,11 +123,17 @@ def assert_template(options = {}, message = nil)


if expected_partial = options[:partial] if expected_partial = options[:partial]
if expected_locals = options[:locals] if expected_locals = options[:locals]
if defined?(@locals) if defined?(@_locals)
actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')] actual_locals_collection = @_locals[expected_partial.to_s.sub(/^_/,'')]
expected_locals.each_pair do |k,v| result = actual_locals_collection.any? do |actual_locals|
assert_equal(v, actual_locals[k]) expected_locals.each_pair.all? do |k,v|
v == actual_locals[k]
end
end end
msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
expected_locals,
actual_locals_collection]
assert(result, msg)
else else
warn "the :locals option to #assert_template is only supported in a ActionView::TestCase" warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
end end
Expand Down
13 changes: 8 additions & 5 deletions actionpack/lib/action_view/test_case.rb
Expand Up @@ -120,7 +120,7 @@ def render(options = {}, local_assigns = {}, &block)
end end


def locals def locals
@locals ||= {} @_locals ||= {}
end end


included do included do
Expand Down Expand Up @@ -162,12 +162,15 @@ def render(options = {}, local_assigns = {})
case options case options
when Hash when Hash
if block_given? if block_given?
locals[options[:layout]] = options[:locals] locals[options[:layout]] ||= []
locals[options[:layout]] << options[:locals]
elsif options.key?(:partial) elsif options.key?(:partial)
locals[options[:partial]] = options[:locals] locals[options[:partial]] ||= []
locals[options[:partial]] << options[:locals]
end end
else else
locals[options] = local_assigns locals[options] ||= []
locals[options] << local_assigns
end end


super super
Expand Down Expand Up @@ -197,7 +200,7 @@ def view
:@_routes, :@_routes,
:@controller, :@controller,
:@_layouts, :@_layouts,
:@locals, :@_locals,
:@method_name, :@method_name,
:@output_buffer, :@output_buffer,
:@_partials, :@_partials,
Expand Down
2 changes: 2 additions & 0 deletions actionpack/test/fixtures/test/render_two_partials.html.erb
@@ -0,0 +1,2 @@
<%= render :partial => 'partial', :locals => {'first' => '1'} %>
<%= render :partial => 'partial', :locals => {'second' => '2'} %>
8 changes: 8 additions & 0 deletions actionpack/test/template/test_case_test.rb
Expand Up @@ -321,6 +321,14 @@ class RenderTemplateTest < ActionView::TestCase
assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "Somebody Else" } assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "Somebody Else" }
end end
end end

test 'supports different locals on the same partial' do
controller.controller_path = "test"
render(:template => "test/render_two_partials")
assert_template partial: '_partial', locals: { 'first' => '1' }
assert_template partial: '_partial', locals: { 'second' => '2' }
end

end end


module AHelperWithInitialize module AHelperWithInitialize
Expand Down

0 comments on commit ed95674

Please sign in to comment.