Skip to content
This repository
Browse code

Check for uninitialized instance variables

  • Loading branch information...
commit dc0411fad78bfc92fe92dc88bbad726eb4d1a883 1 parent 8b4461c
Jeremy Kemper jeremy authored
2  actionpack/lib/action_controller/base.rb
@@ -453,7 +453,7 @@ def view_paths=(value)
453 453 # ArticleController.prepend_view_path(["views/default", "views/custom"])
454 454 #
455 455 def prepend_view_path(path)
456   - @view_paths = superclass.view_paths.dup if @view_paths.nil?
  456 + @view_paths = superclass.view_paths.dup if !defined?(@view_paths) || @view_paths.nil?
457 457 @view_paths.unshift(*path)
458 458 end
459 459
10 actionpack/lib/action_view/base.rb
@@ -278,9 +278,9 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc:
278 278 # the same name but differing formats. See +Request#template_format+
279 279 # for more details.
280 280 def template_format
281   - return @template_format if @template_format
282   -
283   - if controller && controller.respond_to?(:request)
  281 + if defined? @template_format
  282 + @template_format
  283 + elsif controller && controller.respond_to?(:request)
284 284 @template_format = controller.request.template_format
285 285 else
286 286 @template_format = :html
@@ -366,7 +366,9 @@ def _render_with_layout(options, local_assigns, &block) #:nodoc:
366 366 end
367 367 else
368 368 begin
369   - original_content_for_layout, @content_for_layout = @content_for_layout, render(options)
  369 + original_content_for_layout = @content_for_layout if defined?(@content_for_layout)
  370 + @content_for_layout = render(options)
  371 +
370 372 if (options[:inline] || options[:file] || options[:text])
371 373 @cached_content_for_layout = @content_for_layout
372 374 render(:file => partial_layout, :locals => local_assigns)
14 actionpack/lib/action_view/renderable.rb
... ... @@ -1,8 +1,7 @@
1 1 module ActionView
2   - module Renderable
3   - # NOTE: The template that this mixin is beening include into is frozen
4   - # So you can not set or modify any instance variables
5   -
  2 + # NOTE: The template that this mixin is being included into is frozen
  3 + # so you cannot set or modify any instance variables
  4 + module Renderable #:nodoc:
6 5 extend ActiveSupport::Memoizable
7 6
8 7 def self.included(base)
@@ -33,10 +32,11 @@ def render(view, local_assigns = {})
33 32 view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type)
34 33
35 34 view.send(method_name(local_assigns), local_assigns) do |*names|
36   - if proc = view.instance_variable_get("@_proc_for_layout")
  35 + ivar = :@_proc_for_layout
  36 + if view.instance_variable_defined?(ivar) and proc = view.instance_variable_get(ivar)
37 37 view.capture(*names, &proc)
38   - else
39   - view.instance_variable_get("@content_for_#{names.first || 'layout'}")
  38 + elsif view.instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}")
  39 + view.instance_variable_get(ivar)
40 40 end
41 41 end
42 42 end
18 actionpack/lib/action_view/renderable_partial.rb
... ... @@ -1,8 +1,7 @@
1 1 module ActionView
2   - module RenderablePartial
3   - # NOTE: The template that this mixin is beening include into is frozen
4   - # So you can not set or modify any instance variables
5   -
  2 + # NOTE: The template that this mixin is being included into is frozen
  3 + # so you cannot set or modify any instance variables
  4 + module RenderablePartial #:nodoc:
6 5 extend ActiveSupport::Memoizable
7 6
8 7 def variable_name
@@ -30,10 +29,13 @@ def render_partial(view, object = nil, local_assigns = {}, as = nil)
30 29 local_assigns[variable_name]
31 30
32 31 if view.respond_to?(:controller)
33   - object ||= ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
34   - view.controller.instance_variable_get("@#{variable_name}"),
35   - "@#{variable_name} will no longer be implicitly assigned to #{variable_name}"
36   - )
  32 + ivar = :"@#{variable_name}"
  33 + object ||=
  34 + if view.controller.instance_variable_defined?(ivar)
  35 + ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
  36 + view.controller.instance_variable_get(ivar),
  37 + "#{ivar} will no longer be implicitly assigned to #{variable_name}")
  38 + end
37 39 end
38 40
39 41 # Ensure correct object is reassigned to other accessors

2 comments on commit dc0411f

Ryan Lowe

I’m updating to Rails 2.2 and I got a lot of deprecation messages like:

DEPRECATION WARNING: @change will no longer be implicitly assigned to change.

This deprecation check seems to be a little overzealous. If I set an ivar @change in the action and then pass it to a partial with:

:locals => { :change => @change }

then I get this deprecation message even if I use change and not @change in the partial. I wasn’t taking advantage of an implicit assignment. If I don’t use @change in the partial because I only use the local change, should I still get this deprecation message?

Michael Koziarski
Owner

Can you open a ticket in lighthouse and then email the core list about this?

It’s entirely possible that this warning is a little over-eager and would be good to catch it.

Please sign in to comment.
Something went wrong with that request. Please try again.