Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

prefix TemplateAssertions ivars (#7459) #7797

Merged
merged 1 commit into from

3 participants

@senny
Owner

I changed the variables @layouts, @partials and @templates to be prefixed with an underscore. The rails tests pass but of course this could make an impact on test cases, which use these internal variables (I don't hope these kind of tests exist though ;)

@senny
Owner

@steveklabnik, @rafaelfranca please have a look. I noticed that there are more internal variables. Should they be prefixed too? I don't think we should try to minimize naming conflicts as much as possible. The current list is:

      INTERNAL_IVARS = [
        :@__name__,
        :@__io__,
        :@_assertion_wrapped,
        :@_assertions,
        :@_result,
        :@_routes,
        :@controller,
        :@_layouts,
        :@locals,
        :@method_name,
        :@output_buffer,
        :@_partials,
        :@passed,
        :@rendered,
        :@request,
        :@routes,
        :@tagged_logger,
        :@_templates,
        :@options,
        :@test_passed,
        :@view,
        :@view_context_class
      ]
@senny
Owner

@rafaelfranca rebased and added a CHANGELOG entry for this one too.

@rafaelfranca
Owner

@senny it seems fine. I don't know about all these variables but one in special took me attention. We have both @_routes and @route in that array. Could you investigate?

I'm merging this now. Thanks.

@rafaelfranca rafaelfranca merged commit f934aec into rails:master
@senny
Owner

@rafaelfranca thanks, I'll have a look at the routing variables.

@senny
Owner

@rafaelfranca I did some research on @routes and @_routes. Currently they are both needed and serve the following purpose:

  • ´@routes´ is the RouteSet instance and is only used for testing
  • ´@_routes´ is from UrlFor, which is mixed into the TestCase. This variable is also used in production

I could submit a PR to rename @routes to @_route_set. What do you think?

@senny
Owner

I just noticed that @routes is accessed from everywhere in the tests. I think we should not touch this code.

@rafaelfranca
Owner

hmm. So I think we can't do anything :(

@steveklabnik
Collaborator

At least for now... yay encapsulation! :(

@rafaelfranca rafaelfranca referenced this pull request from a commit
@rafaelfranca rafaelfranca Revert "Merge pull request #7797 from senny/7459_prefix_tempalte_asse…
…rtion_variables"

This reverts commit 2bad605.

Conflicts:
	actionpack/CHANGELOG.md

Reason: This added a regression related with shoulda-matchers, since it
is expecting the instance variable @layouts

See https://github.com/thoughtbot/shoulda-matchers/blob/9e1188eea68c47d9a56ce6280e45027da6187ab1/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb#L74

This will introduce back #7459 but this stable release will be backward compatible.
Related with #8068.
6b7cd20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 1, 2012
  1. @senny

    prefix TemplateAssertions ivars (#7459)

    senny authored senny committed
This page is out of date. Refresh to see the latest.
View
6 actionpack/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* Rename internal variables on ActionController::TemplateAssertions to prevent
+ naming collisions. @partials, @templates and @layouts are now prefixed with an underscore.
+ Fix #7459
+
+ *Yves Senn*
+
* `resource` and `resources` don't modify the passed options hash
Fix #7777
View
40 actionpack/lib/action_controller/test_case.rb
@@ -12,16 +12,16 @@ module TemplateAssertions
end
def setup_subscriptions
- @partials = Hash.new(0)
- @templates = Hash.new(0)
- @layouts = Hash.new(0)
+ @_partials = Hash.new(0)
+ @_templates = Hash.new(0)
+ @_layouts = Hash.new(0)
ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload|
path = payload[:layout]
if path
- @layouts[path] += 1
+ @_layouts[path] += 1
if path =~ /^layouts\/(.*)/
- @layouts[$1] += 1
+ @_layouts[$1] += 1
end
end
end
@@ -32,11 +32,11 @@ def setup_subscriptions
partial = path =~ /^.*\/_[^\/]*$/
if partial
- @partials[path] += 1
- @partials[path.split("/").last] += 1
+ @_partials[path] += 1
+ @_partials[path.split("/").last] += 1
end
- @templates[path] += 1
+ @_templates[path] += 1
end
end
@@ -46,9 +46,9 @@ def teardown_subscriptions
end
def process(*args)
- @partials = Hash.new(0)
- @templates = Hash.new(0)
- @layouts = Hash.new(0)
+ @_partials = Hash.new(0)
+ @_templates = Hash.new(0)
+ @_layouts = Hash.new(0)
super
end
@@ -88,7 +88,7 @@ def assert_template(options = {}, message = nil)
case options
when NilClass, Regexp, String, Symbol
options = options.to_s if Symbol === options
- rendered = @templates
+ rendered = @_templates
msg = message || sprintf("expecting <%s> but rendering with <%s>",
options.inspect, rendered.keys)
matches_template =
@@ -109,15 +109,15 @@ def assert_template(options = {}, message = nil)
if options.key?(:layout)
expected_layout = options[:layout]
msg = message || sprintf("expecting layout <%s> but action rendered <%s>",
- expected_layout, @layouts.keys)
+ expected_layout, @_layouts.keys)
case expected_layout
when String, Symbol
- assert_includes @layouts.keys, expected_layout.to_s, msg
+ assert_includes @_layouts.keys, expected_layout.to_s, msg
when Regexp
- assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg)
+ assert(@_layouts.keys.any? {|l| l =~ expected_layout }, msg)
when nil, false
- assert(@layouts.empty?, msg)
+ assert(@_layouts.empty?, msg)
end
end
@@ -128,17 +128,17 @@ def assert_template(options = {}, message = nil)
assert_equal(v, actual_locals[k])
end
elsif expected_count = options[:count]
- actual_count = @partials[expected_partial]
+ actual_count = @_partials[expected_partial]
msg = message || sprintf("expecting %s to be rendered %s time(s) but rendered %s time(s)",
expected_partial, expected_count, actual_count)
assert(actual_count == expected_count.to_i, msg)
else
msg = message || sprintf("expecting partial <%s> but action rendered <%s>",
- options[:partial], @partials.keys)
- assert_includes @partials, expected_partial, msg
+ options[:partial], @_partials.keys)
+ assert_includes @_partials, expected_partial, msg
end
elsif options.key?(:partial)
- assert @partials.empty?,
+ assert @_partials.empty?,
"Expected no partials to be rendered"
end
else
View
6 actionpack/lib/action_view/test_case.rb
@@ -196,17 +196,17 @@ def view
:@_result,
:@_routes,
:@controller,
- :@layouts,
+ :@_layouts,
:@locals,
:@method_name,
:@output_buffer,
- :@partials,
+ :@_partials,
:@passed,
:@rendered,
:@request,
:@routes,
:@tagged_logger,
- :@templates,
+ :@_templates,
:@options,
:@test_passed,
:@view,
Something went wrong with that request. Please try again.