Skip to content

Commit

Permalink
Fix not auto-corrected for Rails/DuplicateAssociation
Browse files Browse the repository at this point in the history
This PR is fixed not auto-corrected case for `Rails/DuplicateAssociation`

The following problems that could'nt be corrected automatically
when exactly the same association was defined have been resolved.

```ruby
class Post < ApplicationRecord
  belongs_to :foo
  belongs_to :bar
  belongs_to :foo
end
```
  • Loading branch information
ydah committed May 11, 2022
1 parent 15c1227 commit 98bd282
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#703](https://github.com/rubocop/rubocop-rails/pull/703): Fix not auto-corrected for `Rails/DuplicateAssociation`. ([@ydah][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/duplicate_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def on_class(class_node)
offenses(class_node).each do |name, nodes|
nodes.each do |node|
add_offense(node, message: format(MSG, name: name)) do |corrector|
next if nodes.last == node
next if same_line?(nodes.last, node)

corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
Expand Down
84 changes: 84 additions & 0 deletions spec/rubocop/cop/rails/duplicate_association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
RSpec.describe RuboCop::Cop::Rails::DuplicateAssociation, :config do
describe 'belongs_to' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foo
^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
belongs_to :bar
belongs_to :foo
^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
belongs_to :blah
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
belongs_to :bar
belongs_to :foo
belongs_to :blah
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
belongs_to :foo
Expand All @@ -26,6 +47,27 @@ class Post < ApplicationRecord

describe 'has_many' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_many :foos
^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_many :bars
has_many :foos
^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_many :blahs
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_many :bars
has_many :foos
has_many :blahs
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_many :foos
Expand All @@ -49,6 +91,27 @@ class Post < ApplicationRecord

describe 'has_one' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_one :foo
^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
has_one :bar
has_one :foo
^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations.
has_one :blah
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_one :bar
has_one :foo
has_one :blah
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_one :foo
Expand All @@ -72,6 +135,27 @@ class Post < ApplicationRecord

describe 'has_and_belongs_to_many' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :foos
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations.
has_and_belongs_to_many :blahs
end
RUBY

expect_correction(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :bars
has_and_belongs_to_many :foos
has_and_belongs_to_many :blahs
end
RUBY
end

it 'registers an offense with scope block' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
has_and_belongs_to_many :foos
Expand Down

0 comments on commit 98bd282

Please sign in to comment.