Skip to content

Commit a54c273

Browse files
authored
Merge pull request #29 from andyw8/helper-instance-variable
Avoid instance variables in helpers
2 parents eeafe2e + 2c00a6b commit a54c273

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

config/default.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ Rails/HasManyOrHasOneDependent:
103103
Include:
104104
- app/models/**/*.rb
105105

106+
Rails/HelperInstanceVariable:
107+
Include:
108+
- app/helpers/**/*.rb
109+
106110
Rails/HttpStatus:
107111
EnforcedStyle: symbolic
108112
SupportedStyles:

config/enabled.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Rails/HasManyOrHasOneDependent:
102102
StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#has_many-has_one-dependent-option'
103103
Enabled: true
104104

105+
Rails/HelperInstanceVariable:
106+
Description: 'Do not use instance variables in helpers'
107+
Enabled: true
108+
105109
Rails/HttpPositionalArguments:
106110
Description: 'Use keyword arguments instead of positional arguments in http method calls.'
107111
Enabled: true
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

lib/rubocop/cop/rails_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Cop
2929
require_relative 'rails/find_each'
3030
require_relative 'rails/has_and_belongs_to_many'
3131
require_relative 'rails/has_many_or_has_one_dependent'
32+
require_relative 'rails/helper_instance_variable'
3233
require_relative 'rails/http_positional_arguments'
3334
require_relative 'rails/http_status'
3435
require_relative 'rails/inverse_of'

manual/cops_rails.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,42 @@ Include | `app/models/**/*.rb` | Array
804804

805805
* [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)
806806

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+
807843
## Rails/HttpPositionalArguments
808844

809845
Enabled by default | Supports autocorrection
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

0 commit comments

Comments
 (0)