Permalink
Browse files

Do not generate local vars for partials without object or collection

Previously rendering a partial without giving :object or :collection
would generate a local variable with the partial name by default.

This was noticed due to warnings in Ruby 2.0 of not used variables,
which turned out to be the generation of not used variables inside
partials that do not contain objects related to them.
  • Loading branch information...
1 parent 77516a7 commit c67005f221f102fe2caca231027d9b11cf630484 @carlosantoniodasilva carlosantoniodasilva committed Dec 12, 2012
@@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* Do not generate local variables for partials without object or collection.
+ Previously rendering a partial without giving `:object` or `:collection`
+ would generate a local variable with the partial name by default.
+
+ *Carlos Antonio da Silva*
+
* Return the last valid, non-private IP address from the X-Forwarded-For,
Client-IP and Remote-Addr headers, in that order. Document the rationale
for that decision, and describe the options that can be passed to the
@@ -452,7 +452,7 @@ def merge_prefix_into_object_path(prefix, object_path)
def retrieve_template_keys
keys = @locals.keys
- keys << @variable
+ keys << @variable if @object || @collection
keys << @variable_counter if @collection
keys
end
@@ -0,0 +1 @@
+<%= partial_name_local_variable %>
@@ -318,6 +318,13 @@ def test_render_partial_using_collection
@controller_view.render(customers, :greeting => "Hello")
end
+ def test_render_partial_without_object_or_collection_does_not_generate_partial_name_local_variable
+ exception = assert_raises ActionView::Template::Error do
+ @controller_view.render("partial_name_local_variable")
+ end
+ assert_match "undefined local variable or method `partial_name_local_variable'", exception.message
+ end
+
# TODO: The reason for this test is unclear, improve documentation
def test_render_partial_and_fallback_to_layout
assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })

2 comments on commit c67005f

@tilsammans
Contributor

Lots of documentation from the Rails 2.x days where this magic variable is mentioned. I think relying on it has become less common, but it is still a pretty big change when it's no longer there. It will cause confusion for sure. Is there a way to print a deprecation notice when this variable is accessed?

Please sign in to comment.