cancancan 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64c041d800f42e86e488e7c0f46bad09c21ea0d075142d0693deed634feb1c37
4
- data.tar.gz: 78fbf14fd5a661c92c76bd9204e7996c096cec7f82e1b2fd1f73d33acb23d99b
3
+ metadata.gz: c4498ac94e1994faa4da80dc957d8c9564433d991048774f9ac2f051e60de580
4
+ data.tar.gz: 74209123c4c49adcd1d2d81df1de61c5f8cc2f243fdcdb4d01d3e41731e4c266
5
5
  SHA512:
6
- metadata.gz: 95f9e1b7c6bb47b5d8ed2172752f8c7e7d9c4fb4567e1b0f43a6bf97eae4ed7d4b2557e85a24ce92ddd526e870b538e59d05767a62705a072a2532208d81cd67
7
- data.tar.gz: 0b24f5078921f372af6fa98d180cc1f9dd79c6d17cd502fac4bdd3e87ce196e2a714070133d63caaf916840ad5bc70c938a435949f1d50889b2dd5a89fde590c
6
+ metadata.gz: eb7774650d12a7073d09bb713f7eedfd7d376689f6d6bb842620b325a814720172f4fec900705bcc0dd3bd90818a88d2ab7e3904ea3a5d004533e13b4bed1c4c
7
+ data.tar.gz: a7a6fff07fbd7d52816dd960d00ae0baea7d50c5ed41d0abf32ac3a0c2b6bc3c557a4309944717b57aad785ad7dc394870c079b6a820b62798a373a9d9f8c2b0
@@ -19,12 +19,13 @@ module CanCan
19
19
  end
20
20
 
21
21
  def add_rule_to_index(rule, position)
22
- @rules_index ||= Hash.new { |h, k| h[k] = [] }
22
+ @rules_index ||= {}
23
23
 
24
24
  subjects = rule.subjects.compact
25
25
  subjects << :all if subjects.empty?
26
26
 
27
27
  subjects.each do |subject|
28
+ @rules_index[subject] ||= []
28
29
  @rules_index[subject] << position
29
30
  end
30
31
  end
@@ -48,7 +49,9 @@ module CanCan
48
49
  rules
49
50
  else
50
51
  positions = @rules_index.values_at(subject, *alternative_subjects(subject))
51
- positions.flatten!.sort!
52
+ positions.compact!
53
+ positions.flatten!
54
+ positions.sort!
52
55
  positions.map { |i| @rules[i] }
53
56
  end
54
57
  end
data/lib/cancan/config.rb CHANGED
@@ -21,7 +21,9 @@ module CanCan
21
21
  # `distinct` is not reliable in some cases. See
22
22
  # https://github.com/CanCanCommunity/cancancan/pull/605
23
23
  def self.accessible_by_strategy
24
- @accessible_by_strategy || default_accessible_by_strategy
24
+ return @accessible_by_strategy if @accessible_by_strategy
25
+
26
+ @accessible_by_strategy = default_accessible_by_strategy
25
27
  end
26
28
 
27
29
  def self.default_accessible_by_strategy
@@ -36,9 +38,7 @@ module CanCan
36
38
  end
37
39
 
38
40
  def self.accessible_by_strategy=(value)
39
- unless valid_accessible_by_strategies.include?(value)
40
- raise ArgumentError, "accessible_by_strategy must be one of #{valid_accessible_by_strategies.join(', ')}"
41
- end
41
+ validate_accessible_by_strategy!(value)
42
42
 
43
43
  if value == :subquery && does_not_support_subquery_strategy?
44
44
  raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer'
@@ -47,6 +47,26 @@ module CanCan
47
47
  @accessible_by_strategy = value
48
48
  end
49
49
 
50
+ def self.with_accessible_by_strategy(value)
51
+ return yield if value == accessible_by_strategy
52
+
53
+ validate_accessible_by_strategy!(value)
54
+
55
+ begin
56
+ strategy_was = accessible_by_strategy
57
+ @accessible_by_strategy = value
58
+ yield
59
+ ensure
60
+ @accessible_by_strategy = strategy_was
61
+ end
62
+ end
63
+
64
+ def self.validate_accessible_by_strategy!(value)
65
+ return if valid_accessible_by_strategies.include?(value)
66
+
67
+ raise ArgumentError, "accessible_by_strategy must be one of #{valid_accessible_by_strategies.join(', ')}"
68
+ end
69
+
50
70
  def self.does_not_support_subquery_strategy?
51
71
  !defined?(CanCan::ModelAdapters::ActiveRecordAdapter) ||
52
72
  CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0')
@@ -20,8 +20,10 @@ module CanCan
20
20
  # @articles = Article.accessible_by(current_ability, :update)
21
21
  #
22
22
  # Here only the articles which the user can update are returned.
23
- def accessible_by(ability, action = :index)
24
- ability.model_adapter(self, action).database_records
23
+ def accessible_by(ability, action = :index, strategy: CanCan.accessible_by_strategy)
24
+ CanCan.with_accessible_by_strategy(strategy) do
25
+ ability.model_adapter(self, action).database_records
26
+ end
25
27
  end
26
28
  end
27
29
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CanCan
4
- VERSION = '3.2.2'.freeze
4
+ VERSION = '3.3.0'.freeze
5
5
  end
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.2.2
4
+ version: 3.3.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: 2021-05-27 00:00:00.000000000 Z
14
+ date: 2021-06-21 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: appraisal