File tree Expand file tree Collapse file tree 5 files changed +32
-1
lines changed Expand file tree Collapse file tree 5 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 519
519
[ @leoarnold ] : https://github.com/leoarnold
520
520
[ @TonyArra ] : https://github.com/TonyArra
521
521
[ @tachyons ] : https://github.com/tachyons
522
+ [ @composerinteralia ] : https://github.com/composerinteralia
Original file line number Diff line number Diff line change
1
+ * [ #614 ] ( https://github.com/rubocop/rubocop-rails/pull/614 ) : Add ` IgnoreScopes ` config option for ` Rails/InverseOf ` cop. ([ @composerinteralia ] [ ] )
Original file line number Diff line number Diff line change @@ -453,6 +453,7 @@ Rails/InverseOf:
453
453
Description : ' Checks for associations where the inverse cannot be determined automatically.'
454
454
Enabled : true
455
455
VersionAdded : ' 0.52'
456
+ IgnoreScopes : false
456
457
Include :
457
458
- app/models/**/*.rb
458
459
Original file line number Diff line number Diff line change @@ -126,6 +126,18 @@ module Rails
126
126
# has_many :physicians, through: :appointments
127
127
# end
128
128
#
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
+ #
129
141
# @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
130
142
# @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
131
143
class InverseOf < Base
@@ -189,7 +201,7 @@ def on_send(node)
189
201
end
190
202
191
203
def scope? ( arguments )
192
- arguments . any? ( &:block_type? )
204
+ ! ignore_scopes? && arguments . any? ( &:block_type? )
193
205
end
194
206
195
207
def options_requiring_inverse_of? ( options )
@@ -236,6 +248,10 @@ def message(options)
236
248
SPECIFY_MSG
237
249
end
238
250
end
251
+
252
+ def ignore_scopes?
253
+ cop_config [ 'IgnoreScopes' ] == true
254
+ end
239
255
end
240
256
end
241
257
end
Original file line number Diff line number Diff line change @@ -25,6 +25,18 @@ class Person
25
25
end
26
26
RUBY
27
27
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
28
40
end
29
41
30
42
context 'with option preventing automatic inverse' do
You can’t perform that action at this time.
0 commit comments