Skip to content

Commit

Permalink
Allow ignoring scopes for inverse_of cop
Browse files Browse the repository at this point in the history
With rails/rails#43358, Rails can now infer
inverse_of for associations with a scope when
`config.active_record.automatic_scope_inversing = true`.

This commit adds an `IgnoreScopes` option to the inverse_of cop allowing
us to disable the cop for scopes when the new Rails option is set to
`true`.

I considered having the default for `IgnoreScopes` change based on the
Rails version since this is the default for new Rails >= 7.0
applications, but existing applications that upgrade to 7.0 wouldn't
necessarily have the option set and in that case changing the
rubocop-rails default behavior would be a breaking change.
  • Loading branch information
composerinteralia committed Jan 10, 2022
1 parent 07cfcb4 commit 7eecd2c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,4 @@
[@leoarnold]: https://github.com/leoarnold
[@TonyArra]: https://github.com/TonyArra
[@tachyons]: https://github.com/tachyons
[@composerinteralia]: https://github.com/composerinteralia
1 change: 1 addition & 0 deletions changelog/new_allow_ignoring_scopes_for_inverse_of_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#614](https://github.com/rubocop/rubocop-rails/pull/614): Add `IgnoreScopes` config option for `Rails/InverseOf` cop. ([@composerinteralia][])
18 changes: 16 additions & 2 deletions lib/rubocop/cop/rails/inverse_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Rails
# belongs_to :blog
# end
#
# @example
# @example IgnoreScopes: false (default)
# # bad
# class Blog < ApplicationRecord
# has_many :posts, -> { order(published_at: :desc) }
Expand Down Expand Up @@ -65,6 +65,16 @@ module Rails
# inverse_of: false)
# end
#
# @example IgnoreScopes: true
# # good
# class Blog < ApplicationRecord
# has_many :posts, -> { order(published_at: :desc) }
# end
#
# class Post < ApplicationRecord
# belongs_to :blog
# end
#
# @example
# # bad
# class Picture < ApplicationRecord
Expand Down Expand Up @@ -189,7 +199,7 @@ def on_send(node)
end

def scope?(arguments)
arguments.any?(&:block_type?)
!ignore_scopes? && arguments.any?(&:block_type?)
end

def options_requiring_inverse_of?(options)
Expand Down Expand Up @@ -236,6 +246,10 @@ def message(options)
SPECIFY_MSG
end
end

def ignore_scopes?
cop_config['IgnoreScopes'] == true
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rails/inverse_of_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ class Person
end
RUBY
end

context 'when `IgnoreScopes: true`' do
let(:cop_config) do
{ 'IgnoreScopes' => true }
end

it 'does not register an offense when not specifying `:inverse_of`' do
expect_no_offenses(
'has_many :foo, -> () { where(bar: true) }'
)
end
end
end

context 'with option preventing automatic inverse' do
Expand Down

0 comments on commit 7eecd2c

Please sign in to comment.