1
1
# frozen_string_literal: true
2
2
3
+ # rubocop:disable Metrics/AbcSize
4
+ # rubocop:disable Metrics/CyclomaticComplexity
5
+ # rubocop:disable Metrics/PerceivedComplexity
3
6
module CanCan
4
7
module ModelAdapters
5
8
class ActiveRecordAdapter < AbstractAdapter
@@ -52,7 +55,7 @@ def parent_child_conditions(parent, child, all_conditions)
52
55
# Search again in case of polymorphic associations, this time matching on the :has_many side
53
56
# via the :as option, as well as klass
54
57
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 )
56
59
end &.foreign_key &.to_sym
57
60
58
61
foreign_key . nil? ? nil : all_conditions [ foreign_key ]
@@ -61,7 +64,7 @@ def parent_child_conditions(parent, child, all_conditions)
61
64
def matching_parent_child_polymorphic_association ( parent_assoc , child_class )
62
65
return nil unless parent_assoc . klass == child_class
63
66
return nil if parent_assoc &.options [ :as ] . nil?
64
-
67
+
65
68
child_class . reflect_on_all_associations ( :belongs_to ) . find do |child_assoc |
66
69
# Only match this way for polymorphic associations
67
70
child_assoc . polymorphic? && child_assoc . name == parent_assoc . options [ :as ]
@@ -72,12 +75,12 @@ def child_association_to_parent(parent, child)
72
75
child_class = child . is_a? ( Class ) ? child : child . class
73
76
parent_class = parent . is_a? ( Class ) ? parent : parent . class
74
77
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 |
76
79
# 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
78
81
end
79
82
80
- return association unless association . nil?
83
+ return association if association
81
84
82
85
parent_class . reflect_on_all_associations ( :has_many ) . each do |has_many_assoc |
83
86
association ||= matching_parent_child_polymorphic_association ( has_many_assoc , child_class )
@@ -217,6 +220,9 @@ def sanitize_sql(conditions)
217
220
end
218
221
end
219
222
end
223
+ # rubocop:enable Metrics/PerceivedComplexity
224
+ # rubocop:enable Metrics/CyclomaticComplexity
225
+ # rubocop:enable Metrics/AbcSize
220
226
221
227
ActiveSupport . on_load ( :active_record ) do
222
228
send :include , CanCan ::ModelAdditions
0 commit comments