Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rails/DefaultScope cop #267

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#283](https://github.com/rubocop-hq/rubocop-rails/pull/283): Add new `Rails/FindById` cop. ([@fatkodima][])
* [#276](https://github.com/rubocop-hq/rubocop-rails/pull/276): Add new `Rails/RenderPlainText` cop. ([@fatkodima][])
* [#76](https://github.com/rubocop-hq/rubocop-rails/issues/76): Add new `Rails/DefaultScope` cop. ([@fatkodima][])
* [#275](https://github.com/rubocop-hq/rubocop-rails/pull/275): Add new `Rails/MatchRoute` cop. ([@fatkodima][])
* [#271](https://github.com/rubocop-hq/rubocop-rails/pull/271): Add new `Rails/RenderInline` cop. ([@fatkodima][])
* [#281](https://github.com/rubocop-hq/rubocop-rails/pull/281): Add new `Rails/MailerName` cop. ([@fatkodima][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ Rails/Date:
- strict
- flexible

Rails/DefaultScope:
Description: 'Avoid use of `default_scope`.'
StyleGuide: 'https://rails.rubystyle.guide#avoid-default-scope'
Enabled: false
VersionAdded: '2.7'

Rails/Delegate:
Description: 'Prefer delegate method for delegations.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* xref:cops_rails.adoc#railscontenttag[Rails/ContentTag]
* xref:cops_rails.adoc#railscreatetablewithtimestamps[Rails/CreateTableWithTimestamps]
* xref:cops_rails.adoc#railsdate[Rails/Date]
* xref:cops_rails.adoc#railsdefaultscope[Rails/DefaultScope]
* xref:cops_rails.adoc#railsdelegate[Rails/Delegate]
* xref:cops_rails.adoc#railsdelegateallowblank[Rails/DelegateAllowBlank]
* xref:cops_rails.adoc#railsdynamicfindby[Rails/DynamicFindBy]
Expand Down
39 changes: 39 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,45 @@ date.in_time_zone
| `strict`, `flexible`
|===

== Rails/DefaultScope

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Disabled
| Yes
| No
| 2.7
| -
|===

This cop looks for uses of `default_scope`.

=== Examples

[source,ruby]
----
# bad
default_scope -> { where(hidden: false) }

# good
scope :published, -> { where(hidden: false) }

# bad
def self.default_scope
where(hidden: false)
end

# good
def self.published
where(hidden: false)
end
----

=== References

* https://rails.rubystyle.guide#avoid-default-scope

== Rails/Delegate

|===
Expand Down
54 changes: 54 additions & 0 deletions lib/rubocop/cop/rails/default_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# This cop looks for uses of `default_scope`.
#
# @example
# # bad
# default_scope -> { where(hidden: false) }
#
# # good
# scope :published, -> { where(hidden: false) }
#
# # bad
# def self.default_scope
# where(hidden: false)
# end
#
# # good
# def self.published
# where(hidden: false)
# end
#
class DefaultScope < Cop
MSG = 'Avoid use of `default_scope`. It is better to use explicitly named scopes.'

def_node_matcher :method_call?, <<~PATTERN
(send nil? :default_scope ...)
PATTERN

def_node_matcher :class_method_definition?, <<~PATTERN
(defs _ :default_scope args ...)
PATTERN

def_node_matcher :eigenclass_method_definition?, <<~PATTERN
(sclass (self) $(def :default_scope args ...))
PATTERN

def on_send(node)
add_offense(node, location: :selector) if method_call?(node)
end

def on_defs(node)
add_offense(node, location: :name) if class_method_definition?(node)
end

def on_sclass(node)
eigenclass_method_definition?(node) { |default_scope| add_offense(default_scope, location: :name) }
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_relative 'rails/content_tag'
require_relative 'rails/create_table_with_timestamps'
require_relative 'rails/date'
require_relative 'rails/default_scope'
require_relative 'rails/delegate'
require_relative 'rails/delegate_allow_blank'
require_relative 'rails/dynamic_find_by'
Expand Down
51 changes: 51 additions & 0 deletions spec/rubocop/cop/rails/default_scope_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::DefaultScope do
subject(:cop) { described_class.new }

it 'registers an offense when calling `default_scope` within class' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
default_scope -> { where(hidden: false) }
^^^^^^^^^^^^^ Avoid use of `default_scope`. It is better to use explicitly named scopes.
end
RUBY
end

it 'registers an offense when defining `default_scope` as class method' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
def self.default_scope
^^^^^^^^^^^^^ Avoid use of `default_scope`. It is better to use explicitly named scopes.
end
end
RUBY
end

it 'registers an offense when defining `default_scope` as eigenclass method' do
expect_offense(<<~RUBY)
class Post < ApplicationRecord
class << self
def default_scope
^^^^^^^^^^^^^ Avoid use of `default_scope`. It is better to use explicitly named scopes.
end
end
end
RUBY
end

it 'does not register an offense when defining `default_scope` instance method' do
expect_no_offenses(<<~RUBY)
class Post < ApplicationRecord
def default_scope
end
end
RUBY
end

it 'does not register an offense when calling `default_scope` on local variable receiver' do
expect_no_offenses(<<~RUBY)
foo.default_scope
RUBY
end
end