Skip to content

Commit

Permalink
[Fix rubocop#1143] Fix an error for `Rails/RedundantActiveRecordAllMe…
Browse files Browse the repository at this point in the history
…thod`

Fixes rubocop#1143.

This PR fixes an error for `Rails/RedundantActiveRecordAllMethod`
when using RuboCop 1.51 or lower.
  • Loading branch information
koic committed Oct 26, 2023
1 parent edeaa45 commit 89c2135
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1143](https://github.com/rubocop/rubocop-rails/issues/1143): Fix an error for `Rails/RedundantActiveRecordAllMethod` when using RuboCop 1.51 or lower. ([@koic][])
29 changes: 29 additions & 0 deletions lib/rubocop/cop/rails/redundant_active_record_all_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ def possible_enumerable_block_method?(node)
def offense_range(node)
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end

# TODO: In the future, please support only RuboCop 1.52+ and use `RuboCop::Cop::AllowedReceivers`:
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cop/mixin/allowed_receivers.rb
# At that time, this duplicated module implementation can be removed.
module AllowedReceivers
def allowed_receiver?(receiver)
receiver_name = receiver_name(receiver)

allowed_receivers.include?(receiver_name)
end

def receiver_name(receiver)
return receiver_name(receiver.receiver) if receiver.receiver && !receiver.receiver.const_type?

if receiver.send_type?
if receiver.receiver
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
else
receiver.method_name.to_s
end
else
receiver.source
end
end

def allowed_receivers
cop_config.fetch('AllowedReceivers', [])
end
end
end
end
end
Expand Down

0 comments on commit 89c2135

Please sign in to comment.