Class: RuboCop::Cop::Rails::HelperInstanceVariable

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rails/helper_instance_variable.rb

Overview

This cop checks for use of the helper methods which reference instance variables.

Relying on instance variables makes it difficult to re-use helper methods.

If it seems awkward to explicitly pass in each dependent variable, consider moving the behaviour elsewhere, for example to a model, decorator or presenter.

Examples:

# bad
def welcome_message
  "Hello #{@user.name}"
end

# good
def welcome_message(user)
  "Hello #{user.name}"
end

Constant Summary collapse

MSG =
'Do not use instance variables in helpers.'

Instance Method Summary collapse

Instance Method Details

#on_ivar(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/rails/helper_instance_variable.rb', line 29

def on_ivar(node)
  add_offense(node)
end

#on_ivasgn(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/rails/helper_instance_variable.rb', line 33

def on_ivasgn(node)
  add_offense(node, location: :name)
end