Skip to content

Commit 54945f7

Browse files
committed
Fix rubocop
1 parent f5542ce commit 54945f7

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ jobs:
4444
ruby: '2.7'
4545
- gemfile: 'gemfiles/activerecord_main.gemfile'
4646
ruby: '2.7'
47+
- gemfile: 'gemfiles/activerecord_main.gemfile'
48+
ruby: '3.0'
4749
env:
4850
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
4951

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ AllCops:
5050
- 'gemfiles/**/*'
5151
- 'vendor/**/*'
5252
- 'Appraisals'
53+
- 'node_modules/**/*'

.rubocop_todo.yml

+3
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,6 @@ Style/StringChars:
214214
Style/StringConcatenation:
215215
Exclude:
216216
- 'lib/cancan/rule.rb'
217+
218+
Lint/SafeNavigationChain:
219+
Enabled: false

lib/cancan/model_adapters/active_record_adapter.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
# rubocop:disable Metrics/AbcSize
4+
# rubocop:disable Metrics/CyclomaticComplexity
5+
# rubocop:disable Metrics/PerceivedComplexity
36
module CanCan
47
module ModelAdapters
58
class ActiveRecordAdapter < AbstractAdapter
@@ -52,7 +55,7 @@ def parent_child_conditions(parent, child, all_conditions)
5255
# Search again in case of polymorphic associations, this time matching on the :has_many side
5356
# via the :as option, as well as klass
5457
foreign_key ||= parent_class.reflect_on_all_associations(:has_many).find do |has_many_assoc|
55-
!matching_parent_child_polymorphic_association(has_many_assoc, child_class).nil?
58+
matching_parent_child_polymorphic_association(has_many_assoc, child_class)
5659
end&.foreign_key&.to_sym
5760

5861
foreign_key.nil? ? nil : all_conditions[foreign_key]
@@ -61,7 +64,7 @@ def parent_child_conditions(parent, child, all_conditions)
6164
def matching_parent_child_polymorphic_association(parent_assoc, child_class)
6265
return nil unless parent_assoc.klass == child_class
6366
return nil if parent_assoc&.options[:as].nil?
64-
67+
6568
child_class.reflect_on_all_associations(:belongs_to).find do |child_assoc|
6669
# Only match this way for polymorphic associations
6770
child_assoc.polymorphic? && child_assoc.name == parent_assoc.options[:as]
@@ -72,12 +75,12 @@ def child_association_to_parent(parent, child)
7275
child_class = child.is_a?(Class) ? child : child.class
7376
parent_class = parent.is_a?(Class) ? parent : parent.class
7477

75-
association = child_class.reflect_on_all_associations(:belongs_to).find do |association|
78+
association = child_class.reflect_on_all_associations(:belongs_to).find do |belongs_to_assoc|
7679
# Do not match on polymorphic associations or it will throw an error (klass cannot be determined)
77-
!association.polymorphic? && association.klass == parent.class
80+
!belongs_to_assoc.polymorphic? && belongs_to_assoc.klass == parent.class
7881
end
7982

80-
return association unless association.nil?
83+
return association if association
8184

8285
parent_class.reflect_on_all_associations(:has_many).each do |has_many_assoc|
8386
association ||= matching_parent_child_polymorphic_association(has_many_assoc, child_class)
@@ -217,6 +220,9 @@ def sanitize_sql(conditions)
217220
end
218221
end
219222
end
223+
# rubocop:enable Metrics/PerceivedComplexity
224+
# rubocop:enable Metrics/CyclomaticComplexity
225+
# rubocop:enable Metrics/AbcSize
220226

221227
ActiveSupport.on_load(:active_record) do
222228
send :include, CanCan::ModelAdditions

0 commit comments

Comments
 (0)