-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This AR method does more harm than good, especially in large codebases. rubocop/rubocop-rails#76 lists these bits of evidence against it: * https://rails-bestpractices.com/posts/2013/06/15/default_scope-is-evil/ * https://andycroll.com/ruby/dont-use-default-scope/ * https://stackoverflow.com/questions/25087336/why-is-using-the-rails-default-scope-often-recommend-against/
- Loading branch information
Nick Thomas
committed
Jun 10, 2020
1 parent
dee19c9
commit 4077687
Showing
13 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Cop that blacklists the use of `default_scope`. | ||
class DefaultScope < RuboCop::Cop::Cop | ||
MSG = <<~EOF | ||
Do not use `default_scope`, as it does not follow the principle of | ||
least surprise. See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/33847 | ||
for more details. | ||
EOF | ||
|
||
def_node_matcher :default_scope?, <<~PATTERN | ||
(send {nil? (const nil? ...)} :default_scope ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless default_scope?(node) | ||
|
||
add_offense(node, location: :expression) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'rubocop' | ||
require 'rubocop/rspec/support' | ||
require_relative '../../../rubocop/cop/default_scope' | ||
|
||
describe RuboCop::Cop::DefaultScope do | ||
include CopHelper | ||
|
||
subject(:cop) { described_class.new } | ||
|
||
it 'does not flag the use of default_scope with a send receiver' do | ||
inspect_source('foo.default_scope') | ||
|
||
expect(cop.offenses.size).to eq(0) | ||
end | ||
|
||
it 'flags the use of default_scope with a constant receiver' do | ||
inspect_source('User.default_scope') | ||
|
||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'flags the use of default_scope with a nil receiver' do | ||
inspect_source('class Foo ; default_scope ; end') | ||
|
||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'flags the use of default_scope when passing arguments' do | ||
inspect_source('class Foo ; default_scope(:foo) ; end') | ||
|
||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'flags the use of default_scope when passing a block' do | ||
inspect_source('class Foo ; default_scope { :foo } ; end') | ||
|
||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'ignores the use of default_scope with a local variable receiver' do | ||
inspect_source('users = User.all ; users.default_scope') | ||
|
||
expect(cop.offenses.size).to eq(0) | ||
end | ||
end |