Skip to content

Commit d1443ca

Browse files
authored
Merge pull request #1419 from ydakuka/fix/enhance-rails-duplicate-association-to-handle-alias
[Fix #1356] Enhance `Rails/DuplicateAssociation` to handle alias
2 parents 7024ab5 + 10829d3 commit d1443ca

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][])

lib/rubocop/cop/rails/duplicate_association.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ def on_class(class_node)
6363
private
6464

6565
def register_offense(name, nodes, message_template)
66-
nodes.each do |node|
67-
add_offense(node, message: format(message_template, name: name)) do |corrector|
68-
next if same_line?(nodes.last, node)
66+
last_node = nodes.last
6967

70-
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
68+
nodes.each_with_index do |node, index|
69+
add_offense(node, message: format(message_template, name: name)) do |corrector|
70+
if index.zero?
71+
corrector.replace(node, last_node.source)
72+
else
73+
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
74+
end
7175
end
7276
end
7377
end

spec/rubocop/cop/rails/duplicate_association_spec.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Post < ApplicationRecord
1616

1717
expect_correction(<<~RUBY)
1818
class Post < ApplicationRecord
19-
belongs_to :bar
2019
belongs_to :foo
20+
belongs_to :bar
2121
belongs_to :blah
2222
end
2323
RUBY
@@ -37,8 +37,8 @@ class Post < ApplicationRecord
3737

3838
expect_correction(<<~RUBY)
3939
class Post < ApplicationRecord
40-
belongs_to :bar
4140
belongs_to :foo, -> { active }
41+
belongs_to :bar
4242
belongs_to :blah
4343
end
4444
RUBY
@@ -60,8 +60,8 @@ class Post < ApplicationRecord
6060

6161
expect_correction(<<~RUBY)
6262
class Post < ApplicationRecord
63-
has_many :bars
6463
has_many :foos
64+
has_many :bars
6565
has_many :blahs
6666
end
6767
RUBY
@@ -81,12 +81,31 @@ class Post < ApplicationRecord
8181

8282
expect_correction(<<~RUBY)
8383
class Post < ApplicationRecord
84-
has_many :bars
8584
has_many 'foos', -> { active }
85+
has_many :bars
8686
has_many :blahs
8787
end
8888
RUBY
8989
end
90+
91+
it 'registers an offense with alias' do
92+
expect_offense(<<~RUBY)
93+
class Post < ApplicationRecord
94+
belongs_to :foos, -> { active }
95+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
96+
alias bars foos
97+
belongs_to :foos
98+
^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
99+
end
100+
RUBY
101+
102+
expect_correction(<<~RUBY)
103+
class Post < ApplicationRecord
104+
belongs_to :foos
105+
alias bars foos
106+
end
107+
RUBY
108+
end
90109
end
91110

92111
describe 'has_one' do
@@ -104,8 +123,8 @@ class Post < ApplicationRecord
104123

105124
expect_correction(<<~RUBY)
106125
class Post < ApplicationRecord
107-
has_one :bar
108126
has_one :foo
127+
has_one :bar
109128
has_one :blah
110129
end
111130
RUBY
@@ -125,8 +144,8 @@ class Post < ApplicationRecord
125144

126145
expect_correction(<<~RUBY)
127146
class Post < ApplicationRecord
128-
has_one :bar
129147
has_one :foo, -> { active }
148+
has_one :bar
130149
has_one :blah
131150
end
132151
RUBY
@@ -148,8 +167,8 @@ class Post < ApplicationRecord
148167

149168
expect_correction(<<~RUBY)
150169
class Post < ApplicationRecord
151-
has_and_belongs_to_many :bars
152170
has_and_belongs_to_many :foos
171+
has_and_belongs_to_many :bars
153172
has_and_belongs_to_many :blahs
154173
end
155174
RUBY
@@ -169,8 +188,8 @@ class Post < ApplicationRecord
169188

170189
expect_correction(<<~RUBY)
171190
class Post < ApplicationRecord
172-
has_and_belongs_to_many :bars
173191
has_and_belongs_to_many :foos, -> { active }
192+
has_and_belongs_to_many :bars
174193
has_and_belongs_to_many :blahs
175194
end
176195
RUBY
@@ -200,12 +219,12 @@ class Post < ApplicationRecord
200219

201220
expect_correction(<<~RUBY)
202221
class Post < ApplicationRecord
203-
has_and_belongs_to_many :bars
204222
has_and_belongs_to_many :foos, -> { active }
223+
has_and_belongs_to_many :bars
205224
has_and_belongs_to_many :blahs
206225
207-
has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment'
208226
belongs_to :author
227+
has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment'
209228
end
210229
RUBY
211230
end

0 commit comments

Comments
 (0)