File tree Expand file tree Collapse file tree 6 files changed +115
-0
lines changed Expand file tree Collapse file tree 6 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,10 @@ Rails/HasManyOrHasOneDependent:
103
103
Include :
104
104
- app/models/**/*.rb
105
105
106
+ Rails/HelperInstanceVariable :
107
+ Include :
108
+ - app/helpers/**/*.rb
109
+
106
110
Rails/HttpStatus :
107
111
EnforcedStyle : symbolic
108
112
SupportedStyles :
Original file line number Diff line number Diff line change @@ -102,6 +102,10 @@ Rails/HasManyOrHasOneDependent:
102
102
StyleGuide : ' https://github.com/rubocop-hq/rails-style-guide#has_many-has_one-dependent-option'
103
103
Enabled : true
104
104
105
+ Rails/HelperInstanceVariable :
106
+ Description : ' Do not use instance variables in helpers'
107
+ Enabled : true
108
+
105
109
Rails/HttpPositionalArguments :
106
110
Description : ' Use keyword arguments instead of positional arguments in http method calls.'
107
111
Enabled : true
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for use of the helper methods which reference
7
+ # instance variables.
8
+ #
9
+ # Relying on instance variables makes it difficult to re-use helper
10
+ # methods.
11
+ #
12
+ # If it seems awkward to explicitly pass in each dependent
13
+ # variable, consider moving the behaviour elsewhere, for
14
+ # example to a model, decorator or presenter.
15
+ #
16
+ # @example
17
+ # # bad
18
+ # def welcome_message
19
+ # "Hello #{@user.name}"
20
+ # end
21
+ #
22
+ # # good
23
+ # def welcome_message(user)
24
+ # "Hello #{user.name}"
25
+ # end
26
+ class HelperInstanceVariable < Cop
27
+ MSG = 'Do not use instance variables in helpers.' . freeze
28
+
29
+ def on_ivar ( node )
30
+ add_offense ( node )
31
+ end
32
+
33
+ def on_ivasgn ( node )
34
+ add_offense ( node , location : :name )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ module Cop
29
29
require_relative 'rails/find_each'
30
30
require_relative 'rails/has_and_belongs_to_many'
31
31
require_relative 'rails/has_many_or_has_one_dependent'
32
+ require_relative 'rails/helper_instance_variable'
32
33
require_relative 'rails/http_positional_arguments'
33
34
require_relative 'rails/http_status'
34
35
require_relative 'rails/inverse_of'
Original file line number Diff line number Diff line change @@ -804,6 +804,42 @@ Include | `app/models/**/*.rb` | Array
804
804
805
805
* [ https://github.com/rubocop-hq/rails-style-guide#has_many-has_one-dependent-option ] ( https://github.com/rubocop-hq/rails-style-guide#has_many-has_one-dependent-option )
806
806
807
+ ## Rails/HelperInstanceVariable
808
+
809
+ Enabled by default | Supports autocorrection
810
+ --- | ---
811
+ Enabled | No
812
+
813
+ This cop checks for use of the helper methods which reference
814
+ instance variables.
815
+
816
+ Relying on instance variables makes it difficult to re-use helper
817
+ methods.
818
+
819
+ If it seems awkward to explicitly pass in each dependent
820
+ variable, consider moving the behaviour elsewhere, for
821
+ example to a model, decorator or presenter.
822
+
823
+ ### Examples
824
+
825
+ ``` ruby
826
+ # bad
827
+ def welcome_message
828
+ " Hello #{ @user .name} "
829
+ end
830
+
831
+ # good
832
+ def welcome_message (user )
833
+ " Hello #{ user.name } "
834
+ end
835
+ ```
836
+
837
+ ### Configurable attributes
838
+
839
+ Name | Default value | Configurable values
840
+ --- | --- | ---
841
+ Include | ` app/helpers/**/*.rb ` | Array
842
+
807
843
## Rails/HttpPositionalArguments
808
844
809
845
Enabled by default | Supports autocorrection
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe RuboCop ::Cop ::Rails ::HelperInstanceVariable do
4
+ subject ( :cop ) { described_class . new }
5
+
6
+ it 'reports uses of instance variables' do
7
+ expect_offense ( <<-'RUBY' . strip_indent )
8
+ def welcome_message
9
+ "Hello #{@user.name}"
10
+ ^^^^^ Do not use instance variables in helpers.
11
+ end
12
+ RUBY
13
+ end
14
+
15
+ it 'reports instance variable assignments' do
16
+ expect_offense ( <<-RUBY . strip_indent )
17
+ def welcome_message(user)
18
+ @user_name = user.name
19
+ ^^^^^^^^^^ Do not use instance variables in helpers.
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ specify do
25
+ expect_no_offenses ( <<-'RUBY' . strip_indent )
26
+ def welcome_message(user)
27
+ "Hello #{user.name}"
28
+ end
29
+ RUBY
30
+ end
31
+ end
You can’t perform that action at this time.
0 commit comments