Skip to content

Commit f271b4e

Browse files
authored
Merge pull request #614 from composerinteralia/inverse-of-scope
Allow ignoring scopes for inverse_of cop
2 parents 8ac010a + 95b3e8c commit f271b4e

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,4 @@
519519
[@leoarnold]: https://github.com/leoarnold
520520
[@TonyArra]: https://github.com/TonyArra
521521
[@tachyons]: https://github.com/tachyons
522+
[@composerinteralia]: https://github.com/composerinteralia
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#614](https://github.com/rubocop/rubocop-rails/pull/614): Add `IgnoreScopes` config option for `Rails/InverseOf` cop. ([@composerinteralia][])

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ Rails/InverseOf:
453453
Description: 'Checks for associations where the inverse cannot be determined automatically.'
454454
Enabled: true
455455
VersionAdded: '0.52'
456+
IgnoreScopes: false
456457
Include:
457458
- app/models/**/*.rb
458459

lib/rubocop/cop/rails/inverse_of.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ module Rails
126126
# has_many :physicians, through: :appointments
127127
# end
128128
#
129+
# @example IgnoreScopes: false (default)
130+
# # bad
131+
# class Blog < ApplicationRecord
132+
# has_many :posts, -> { order(published_at: :desc) }
133+
# end
134+
#
135+
# @example IgnoreScopes: true
136+
# # good
137+
# class Blog < ApplicationRecord
138+
# has_many :posts, -> { order(published_at: :desc) }
139+
# end
140+
#
129141
# @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
130142
# @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
131143
class InverseOf < Base
@@ -189,7 +201,7 @@ def on_send(node)
189201
end
190202

191203
def scope?(arguments)
192-
arguments.any?(&:block_type?)
204+
!ignore_scopes? && arguments.any?(&:block_type?)
193205
end
194206

195207
def options_requiring_inverse_of?(options)
@@ -236,6 +248,10 @@ def message(options)
236248
SPECIFY_MSG
237249
end
238250
end
251+
252+
def ignore_scopes?
253+
cop_config['IgnoreScopes'] == true
254+
end
239255
end
240256
end
241257
end

spec/rubocop/cop/rails/inverse_of_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ class Person
2525
end
2626
RUBY
2727
end
28+
29+
context 'when `IgnoreScopes: true`' do
30+
let(:cop_config) do
31+
{ 'IgnoreScopes' => true }
32+
end
33+
34+
it 'does not register an offense when not specifying `:inverse_of`' do
35+
expect_no_offenses(
36+
'has_many :foo, -> () { where(bar: true) }'
37+
)
38+
end
39+
end
2840
end
2941

3042
context 'with option preventing automatic inverse' do

0 commit comments

Comments
 (0)