Skip to content

Commit

Permalink
[Fix #1173] Fix an error for Rails/RedundantActiveRecordAllMethod cop
Browse files Browse the repository at this point in the history
Fixes #1173.

This PR fixes an error for `Rails/RedundantActiveRecordAllMethod` cop
when used with RuboCop 1.51 or lower.
Additionally, it introduces a CI matrix for regression testing with
the oldest supported RuboCop version.
  • Loading branch information
koic committed Oct 31, 2023
1 parent 1be8d21 commit c9acb7a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
oldest_supported_rubocop:
runs-on: ubuntu-latest
name: The oldest supported RuboCop version
steps:
- uses: actions/checkout@v4
- name: Use the oldest supported RuboCop
run: |
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" \
-e "/gem 'rubocop-performance',/d" \
-e "/gem 'rubocop-rspec',/d" -i Gemfile
cat << EOF > Gemfile.local
gem 'rubocop', '1.33.0' # Specify the oldest supported RuboCop version
EOF
- name: set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
bundler-cache: true
- name: spec
run: bundle exec rake spec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1173](https://github.com/rubocop/rubocop-rails/issues/1173): Fix an error for `Rails/RedundantActiveRecordAllMethod` cop when used with RuboCop 1.51 or lower. ([@koic][])
58 changes: 29 additions & 29 deletions lib/rubocop/cop/rails/redundant_active_record_all_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
module RuboCop
module Cop
module Rails
# 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

# Detect redundant `all` used as a receiver for Active Record query methods.
#
# @safety
Expand Down Expand Up @@ -172,35 +201,6 @@ 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 c9acb7a

Please sign in to comment.