Skip to content

Commit 289d8c7

Browse files
authored
Merge pull request #1546 from viralpraxis/change-rails-duplicate-scope-cop-description
Change `Rails/DuplicateScope` cop description
2 parents a6f848a + 7b07498 commit 289d8c7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

config/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Rails/DuplicateAssociation:
408408
VersionChanged: '2.18'
409409

410410
Rails/DuplicateScope:
411-
Description: 'Multiple scopes share this same where clause.'
411+
Description: 'Multiple scopes share this same expression.'
412412
Enabled: pending
413413
Severity: warning
414414
VersionAdded: '2.14'

lib/rubocop/cop/rails/duplicate_scope.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# Checks for multiple scopes in a model that have the same `where` clause. This
6+
# Checks for multiple scopes in a model that have the same expression. This
77
# often means you copy/pasted a scope, updated the name, and forgot to change the condition.
88
#
99
# @example
@@ -19,7 +19,7 @@ module Rails
1919
class DuplicateScope < Base
2020
include ClassSendNodeHelper
2121

22-
MSG = 'Multiple scopes share this same where clause.'
22+
MSG = 'Multiple scopes share this same expression.'
2323

2424
def_node_matcher :scope, <<~PATTERN
2525
(send nil? :scope _ $...)

spec/rubocop/cop/rails/duplicate_scope_spec.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::DuplicateScope, :config do
4-
it 'registers an offense when a duplicate scope is detected' do
4+
it 'registers an offense when a duplicate scope with the same `where` clauses is detected' do
55
expect_offense(<<~RUBY)
66
class Post < ApplicationRecord
77
scope :visible, -> { where(visible: true) }
8-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same where clause.
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same expression.
99
scope :hidden, -> { where(visible: true) }
10-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same where clause.
10+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same expression.
1111
scope :new, -> { where(created_at: 1.week.ago..Date.current) }
1212
scope :popular, -> { where(comments_count: 1000..Float::INFINITY) }
1313
end
1414
RUBY
1515
end
1616

17+
it 'registers an offense when a duplicate scope with the same expression is detected' do
18+
expect_offense(<<~RUBY)
19+
class Post < ApplicationRecord
20+
scope :with_visibility, ->(value) { where(visible: value) }
21+
22+
scope :visible, -> { with_visibility(true) }
23+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same expression.
24+
scope :hidden, -> { with_visibility(true) }
25+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Multiple scopes share this same expression.
26+
end
27+
RUBY
28+
end
29+
1730
it 'does not register an offense when there are no duplicates' do
1831
expect_no_offenses(<<~RUBY)
1932
class Post < ApplicationRecord

0 commit comments

Comments
 (0)