Skip to content

Commit

Permalink
Fix rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
coorasse committed May 23, 2024
1 parent f5542ce commit 54945f7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:
ruby: '2.7'
- gemfile: 'gemfiles/activerecord_main.gemfile'
ruby: '2.7'
- gemfile: 'gemfiles/activerecord_main.gemfile'
ruby: '3.0'
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}

Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ AllCops:
- 'gemfiles/**/*'
- 'vendor/**/*'
- 'Appraisals'
- 'node_modules/**/*'
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,6 @@ Style/StringChars:
Style/StringConcatenation:
Exclude:
- 'lib/cancan/rule.rb'

Lint/SafeNavigationChain:
Enabled: false
16 changes: 11 additions & 5 deletions lib/cancan/model_adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

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

foreign_key.nil? ? nil : all_conditions[foreign_key]
Expand All @@ -61,7 +64,7 @@ def parent_child_conditions(parent, child, all_conditions)
def matching_parent_child_polymorphic_association(parent_assoc, child_class)
return nil unless parent_assoc.klass == child_class
return nil if parent_assoc&.options[:as].nil?

child_class.reflect_on_all_associations(:belongs_to).find do |child_assoc|
# Only match this way for polymorphic associations
child_assoc.polymorphic? && child_assoc.name == parent_assoc.options[:as]
Expand All @@ -72,12 +75,12 @@ def child_association_to_parent(parent, child)
child_class = child.is_a?(Class) ? child : child.class
parent_class = parent.is_a?(Class) ? parent : parent.class

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

return association unless association.nil?
return association if association

parent_class.reflect_on_all_associations(:has_many).each do |has_many_assoc|
association ||= matching_parent_child_polymorphic_association(has_many_assoc, child_class)
Expand Down Expand Up @@ -217,6 +220,9 @@ def sanitize_sql(conditions)
end
end
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/AbcSize

ActiveSupport.on_load(:active_record) do
send :include, CanCan::ModelAdditions
Expand Down

0 comments on commit 54945f7

Please sign in to comment.