Skip to content

Commit

Permalink
Merge pull request #1419 from ydakuka/fix/enhance-rails-duplicate-ass…
Browse files Browse the repository at this point in the history
…ociation-to-handle-alias

[Fix #1356] Enhance `Rails/DuplicateAssociation` to handle alias
  • Loading branch information
koic authored Jan 20, 2025
2 parents 7024ab5 + 10829d3 commit d1443ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1356](https://github.com/rubocop/rubocop-rails/issues/1356): Enhance `Rails/DuplicateAssociation` to handle alias. ([@ydakuka][])
12 changes: 8 additions & 4 deletions lib/rubocop/cop/rails/duplicate_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ def on_class(class_node)
private

def register_offense(name, nodes, message_template)
nodes.each do |node|
add_offense(node, message: format(message_template, name: name)) do |corrector|
next if same_line?(nodes.last, node)
last_node = nodes.last

corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
nodes.each_with_index do |node, index|
add_offense(node, message: format(message_template, name: name)) do |corrector|
if index.zero?
corrector.replace(node, last_node.source)
else
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
end
end
end
Expand Down
39 changes: 29 additions & 10 deletions spec/rubocop/cop/rails/duplicate_association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
belongs_to :bar
belongs_to :foo
belongs_to :bar
belongs_to :blah
end
RUBY
Expand All @@ -37,8 +37,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
belongs_to :bar
belongs_to :foo, -> { active }
belongs_to :bar
belongs_to :blah
end
RUBY
Expand All @@ -60,8 +60,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_many :bars
has_many :foos
has_many :bars
has_many :blahs
end
RUBY
Expand All @@ -81,12 +81,31 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_many :bars
has_many 'foos', -> { active }
has_many :bars
has_many :blahs
end
RUBY
end

it 'registers an offense with alias' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foos, -> { active }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
alias bars foos
belongs_to :foos
^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foos
alias bars foos
end
RUBY
end
end

describe 'has_one' do
Expand All @@ -104,8 +123,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_one :bar
has_one :foo
has_one :bar
has_one :blah
end
RUBY
Expand All @@ -125,8 +144,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_one :bar
has_one :foo, -> { active }
has_one :bar
has_one :blah
end
RUBY
Expand All @@ -148,8 +167,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos
has_and_belongs_to_many :bars
has_and_belongs_to_many :blahs
end
RUBY
Expand All @@ -169,8 +188,8 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos, -> { active }
has_and_belongs_to_many :bars
has_and_belongs_to_many :blahs
end
RUBY
Expand Down Expand Up @@ -200,12 +219,12 @@ class Post < ApplicationRecord

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos, -> { active }
has_and_belongs_to_many :bars
has_and_belongs_to_many :blahs
has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment'
belongs_to :author
has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment'
end
RUBY
end
Expand Down

0 comments on commit d1443ca

Please sign in to comment.