From 89c21358b4b34200c7bc893f1e38c21d4b1200a4 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 26 Oct 2023 17:23:32 +0900 Subject: [PATCH] [Fix #1143] Fix an error for `Rails/RedundantActiveRecordAllMethod` Fixes #1143. This PR fixes an error for `Rails/RedundantActiveRecordAllMethod` when using RuboCop 1.51 or lower. --- ...ails_redundant_active_record_all_method.md | 1 + .../redundant_active_record_all_method.rb | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 changelog/fix_an_error_for_rails_redundant_active_record_all_method.md diff --git a/changelog/fix_an_error_for_rails_redundant_active_record_all_method.md b/changelog/fix_an_error_for_rails_redundant_active_record_all_method.md new file mode 100644 index 0000000000..23cb68fb7f --- /dev/null +++ b/changelog/fix_an_error_for_rails_redundant_active_record_all_method.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb index 8116474563..0684cbe389 100644 --- a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +++ b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb @@ -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