cancancan 3.5.0 → 3.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cancan/model_adapters/active_record_adapter.rb +11 -5
- data/lib/cancan/rules_compressor.rb +18 -0
- data/lib/cancan/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec627804781c87a834f313dfd9447e83d8769449dab9a484f7069cb201a7450a
|
4
|
+
data.tar.gz: d6e99d78e6f075d1870f02c491090d3526e1167770ed886fbd00c1933be78f2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98a14c9573781aad22fe329853e516e9149d977b4f02ee0f274a79651bd29ec6444b15397cff695f2ec8ac94a0a0207b060c11dbe20ab0d65471a18023b0b4e1
|
7
|
+
data.tar.gz: 8e6ac6e2b51fb9ca95c7f59d961d1da0a35d9fa3bae5e8b0fa967c9a2f4c18e44577b8048fa33612919eb03bb010335ccd57c9cffb0497175a906bfc66e4c212
|
@@ -1,5 +1,8 @@
|
|
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 @@ module CanCan
|
|
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
|
-
|
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 @@ module CanCan
|
|
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 @@ module CanCan
|
|
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 |
|
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
|
-
!
|
80
|
+
!belongs_to_assoc.polymorphic? && belongs_to_assoc.klass == parent.class
|
78
81
|
end
|
79
82
|
|
80
|
-
return association
|
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 @@ module CanCan
|
|
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
|
@@ -11,6 +11,7 @@ module CanCan
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def compress(array)
|
14
|
+
array = simplify(array)
|
14
15
|
idx = array.rindex(&:catch_all?)
|
15
16
|
return array unless idx
|
16
17
|
|
@@ -19,5 +20,22 @@ module CanCan
|
|
19
20
|
.drop_while { |n| n.base_behavior == value.base_behavior }
|
20
21
|
.tap { |a| a.unshift(value) unless value.cannot_catch_all? }
|
21
22
|
end
|
23
|
+
|
24
|
+
# If we have A OR (!A AND anything ), then we can simplify to A OR anything
|
25
|
+
# If we have A OR (A OR anything ), then we can simplify to A OR anything
|
26
|
+
# If we have !A AND (A OR something), then we can simplify it to !A AND something
|
27
|
+
# If we have !A AND (!A AND something), then we can simplify it to !A AND something
|
28
|
+
#
|
29
|
+
# So as soon as we see a condition that is the same as the previous one,
|
30
|
+
# we can skip it, no matter of the base_behavior
|
31
|
+
def simplify(rules)
|
32
|
+
seen = Set.new
|
33
|
+
rules.reverse_each.filter_map do |rule|
|
34
|
+
next if seen.include?(rule.conditions)
|
35
|
+
|
36
|
+
seen.add(rule.conditions)
|
37
|
+
rule
|
38
|
+
end.reverse
|
39
|
+
end
|
22
40
|
end
|
23
41
|
end
|
data/lib/cancan/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cancancan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Rodi (Renuo AG)
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2024-05-23 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: appraisal
|
@@ -172,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
172
|
- !ruby/object:Gem::Version
|
173
173
|
version: '0'
|
174
174
|
requirements: []
|
175
|
-
rubygems_version: 3.3.
|
175
|
+
rubygems_version: 3.3.3
|
176
176
|
signing_key:
|
177
177
|
specification_version: 4
|
178
178
|
summary: Simple authorization solution for Rails.
|