forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_instance_variable.rb
39 lines (36 loc) · 968 Bytes
/
helper_instance_variable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# 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.
#
# @example
# # bad
# def welcome_message
# "Hello #{@user.name}"
# end
#
# # good
# def welcome_message(user)
# "Hello #{user.name}"
# end
class HelperInstanceVariable < Cop
MSG = 'Do not use instance variables in helpers.'
def on_ivar(node)
add_offense(node)
end
def on_ivasgn(node)
add_offense(node, location: :name)
end
end
end
end
end