lint: add a Rubocop rule to forbid the use of unscoped
`unscoped` often has an effect broader than intended. For instance: ``` user.dossiers.unscoped.destroy_all ``` will not destroy all the user's dossier, including the hidden ones, but **all the dossiers of all users**. Instead, narrower unscoping should be used: ``` user.dossiers.unscope(where: :hidden).destroy_all ``` (Or even better, use the new `Dossier.with_hidden` scope).
This commit is contained in:
parent
96932faa3f
commit
cf101d64d8
2 changed files with 19 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
require:
|
require:
|
||||||
- rubocop/rspec/focused
|
- rubocop/rspec/focused
|
||||||
|
- ./lib/cops/unscoped.rb
|
||||||
|
|
||||||
AllCops:
|
AllCops:
|
||||||
Exclude:
|
Exclude:
|
||||||
|
|
18
lib/cops/unscoped.rb
Normal file
18
lib/cops/unscoped.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
module RuboCop
|
||||||
|
module Cop
|
||||||
|
module DS
|
||||||
|
class Unscoped < Cop
|
||||||
|
MSG = "Avoid using `unscoped`. Instead unscope specific clauses by using `unscope(where: :attribute)`."
|
||||||
|
|
||||||
|
def_node_matcher :unscoped?, <<-END
|
||||||
|
(send _ :unscoped)
|
||||||
|
END
|
||||||
|
|
||||||
|
def on_send(node)
|
||||||
|
return unless unscoped?(node)
|
||||||
|
add_offense(node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue