Skip to content

Commit 9923143

Browse files
committed
[Fix #1124] Fix false positives for Rails/RedundantActiveRecordAllMethod
Fixes #1124. This PR fixes false positives for `Rails/RedundantActiveRecordAllMethod` when receiver is not an Active Record model.
1 parent 68a0d3d commit 9923143

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1124](https://github.com/rubocop/rubocop-rails/issues/1124): Fix false positives for `Rails/RedundantActiveRecordAllMethod` when receiver is not an Active Record model. ([@koic][])

config/default.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ Rails/RedundantActiveRecordAllMethod:
801801
StyleGuide: 'https://rails.rubystyle.guide/#redundant-all'
802802
Enabled: pending
803803
Safe: false
804+
AllowedReceivers:
805+
- ActionMailer::Preview
806+
- ActiveSupport::TimeZone
804807
VersionAdded: '2.21'
805808

806809
Rails/RedundantAllowNil:

lib/rubocop/cop/rails/redundant_active_record_all_method.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ module Rails
2020
# User.order(:created_at)
2121
# users.where(id: ids)
2222
# user.articles.order(:created_at)
23+
#
24+
# @example AllowedReceivers: ['ActionMailer::Preview', 'ActiveSupport::TimeZone'] (default)
25+
# # good
26+
# ActionMailer::Preview.all.first
27+
# ActiveSupport::TimeZone.all.first
2328
class RedundantActiveRecordAllMethod < Base
2429
include ActiveRecordHelper
30+
include AllowedReceivers
2531
include RangeHelp
2632
extend AutoCorrector
2733

@@ -133,7 +139,7 @@ class RedundantActiveRecordAllMethod < Base
133139

134140
def on_send(node)
135141
return unless followed_by_query_method?(node.parent)
136-
return if node.receiver.nil? && !inherit_active_record_base?(node)
142+
return if node.receiver ? allowed_receiver?(node.receiver) : !inherit_active_record_base?(node)
137143

138144
range_of_all_method = offense_range(node)
139145
add_offense(range_of_all_method) do |collector|

spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,23 @@ class User < ::ActiveRecord::Base
388388
RUBY
389389
end
390390
end
391+
392+
context 'with `AllowedReceivers` config' do
393+
let(:cop_config) do
394+
{ 'AllowedReceivers' => %w[ActionMailer::Preview ActiveSupport::TimeZone] }
395+
end
396+
397+
it 'registers an offense when not using allowed receiver' do
398+
expect_offense(<<~RUBY)
399+
User.all.first
400+
^^^ Redundant `all` detected.
401+
RUBY
402+
end
403+
404+
it 'does not register an offense when using allowed receiver' do
405+
expect_no_offenses(<<~RUBY)
406+
ActiveSupport::TimeZone.all.first
407+
RUBY
408+
end
409+
end
391410
end

0 commit comments

Comments
 (0)