Skip to content

Commit

Permalink
Merge pull request #1174 from koic/fix_error_for_rails_redundant_acti…
Browse files Browse the repository at this point in the history
…ve_record_all_method

[Fix #1173] Fix an error for `Rails/RedundantActiveRecordAllMethod` cop
  • Loading branch information
koic committed Nov 18, 2023
2 parents c176f6b + c9acb7a commit 95bc3e7
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.
#
# NOTE: For the methods `delete_all` and `destroy_all`,
Expand Down Expand Up @@ -185,35 +214,6 @@ def sensitive_association_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 95bc3e7

Please sign in to comment.