Skip to content

Commit d5842ef

Browse files
authored
Merge pull request #1167 from koic/fix_an_error_for_rails_redundant_active_record_all_method
[Fix #1143] Fix an error for `Rails/RedundantActiveRecordAllMethod`
2 parents edeaa45 + 89c2135 commit d5842ef

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1143](https://github.com/rubocop/rubocop-rails/issues/1143): Fix an error for `Rails/RedundantActiveRecordAllMethod` when using RuboCop 1.51 or lower. ([@koic][])

lib/rubocop/cop/rails/redundant_active_record_all_method.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ def possible_enumerable_block_method?(node)
172172
def offense_range(node)
173173
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
174174
end
175+
176+
# TODO: In the future, please support only RuboCop 1.52+ and use `RuboCop::Cop::AllowedReceivers`:
177+
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cop/mixin/allowed_receivers.rb
178+
# At that time, this duplicated module implementation can be removed.
179+
module AllowedReceivers
180+
def allowed_receiver?(receiver)
181+
receiver_name = receiver_name(receiver)
182+
183+
allowed_receivers.include?(receiver_name)
184+
end
185+
186+
def receiver_name(receiver)
187+
return receiver_name(receiver.receiver) if receiver.receiver && !receiver.receiver.const_type?
188+
189+
if receiver.send_type?
190+
if receiver.receiver
191+
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
192+
else
193+
receiver.method_name.to_s
194+
end
195+
else
196+
receiver.source
197+
end
198+
end
199+
200+
def allowed_receivers
201+
cop_config.fetch('AllowedReceivers', [])
202+
end
203+
end
175204
end
176205
end
177206
end

0 commit comments

Comments
 (0)