action_policy 0.5.0 → 0.5.5

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -2
  3. data/config/rubocop-rspec.yml +17 -0
  4. data/lib/.rbnext/1995.next/action_policy/behaviours/policy_for.rb +62 -0
  5. data/lib/.rbnext/1995.next/action_policy/behaviours/scoping.rb +35 -0
  6. data/lib/.rbnext/1995.next/action_policy/policy/authorization.rb +87 -0
  7. data/lib/.rbnext/1995.next/action_policy/utils/pretty_print.rb +159 -0
  8. data/lib/.rbnext/2.7/action_policy/behaviours/policy_for.rb +62 -0
  9. data/lib/.rbnext/2.7/action_policy/i18n.rb +56 -0
  10. data/lib/.rbnext/2.7/action_policy/policy/cache.rb +101 -0
  11. data/lib/.rbnext/2.7/action_policy/policy/pre_check.rb +162 -0
  12. data/lib/.rbnext/2.7/action_policy/rspec/be_authorized_to.rb +89 -0
  13. data/lib/.rbnext/2.7/action_policy/rspec/have_authorized_scope.rb +124 -0
  14. data/lib/.rbnext/2.7/action_policy/utils/pretty_print.rb +159 -0
  15. data/lib/.rbnext/3.0/action_policy/behaviours/thread_memoized.rb +59 -0
  16. data/lib/.rbnext/3.0/action_policy/ext/policy_cache_key.rb +72 -0
  17. data/lib/.rbnext/3.0/action_policy/policy/aliases.rb +69 -0
  18. data/lib/.rbnext/3.0/action_policy/policy/cache.rb +101 -0
  19. data/lib/.rbnext/3.0/action_policy/policy/core.rb +161 -0
  20. data/lib/.rbnext/3.0/action_policy/policy/defaults.rb +31 -0
  21. data/lib/.rbnext/3.0/action_policy/policy/execution_result.rb +37 -0
  22. data/lib/.rbnext/3.0/action_policy/policy/pre_check.rb +162 -0
  23. data/lib/.rbnext/3.0/action_policy/policy/reasons.rb +212 -0
  24. data/lib/.rbnext/3.0/action_policy/rspec/be_authorized_to.rb +89 -0
  25. data/lib/.rbnext/3.0/action_policy/rspec/have_authorized_scope.rb +124 -0
  26. data/lib/.rbnext/3.0/action_policy/utils/pretty_print.rb +159 -0
  27. data/lib/.rbnext/3.0/action_policy/utils/suggest_message.rb +19 -0
  28. data/lib/action_policy/behaviour.rb +2 -2
  29. data/lib/action_policy/behaviours/memoized.rb +1 -1
  30. data/lib/action_policy/behaviours/namespaced.rb +1 -1
  31. data/lib/action_policy/behaviours/policy_for.rb +1 -1
  32. data/lib/action_policy/behaviours/scoping.rb +2 -2
  33. data/lib/action_policy/behaviours/thread_memoized.rb +1 -1
  34. data/lib/action_policy/lookup_chain.rb +11 -27
  35. data/lib/action_policy/policy/aliases.rb +2 -2
  36. data/lib/action_policy/policy/authorization.rb +2 -2
  37. data/lib/action_policy/policy/cache.rb +2 -2
  38. data/lib/action_policy/policy/pre_check.rb +2 -2
  39. data/lib/action_policy/policy/reasons.rb +4 -2
  40. data/lib/action_policy/policy/scoping.rb +2 -2
  41. data/lib/action_policy/test_helper.rb +1 -0
  42. data/lib/action_policy/version.rb +1 -1
  43. metadata +29 -4
@@ -40,7 +40,7 @@ module ActionPolicy
40
40
  base.prepend InstanceMethods
41
41
  end
42
42
 
43
- alias included prepended
43
+ alias_method :included, :prepended
44
44
  end
45
45
 
46
46
  module InstanceMethods # :nodoc:
@@ -36,8 +36,7 @@ module ActionPolicy
36
36
  if strict
37
37
  put_if_absent(:strict, namespace, policy, &block)
38
38
  else
39
- cached_policy = put_if_absent(:strict, namespace, policy, &block)
40
- put_if_absent(:flexible, namespace, policy) { cached_policy }
39
+ put_if_absent(:flexible, namespace, policy, &block)
41
40
  end
42
41
  end
43
42
 
@@ -53,7 +52,7 @@ module ActionPolicy
53
52
  class << self
54
53
  attr_accessor :chain, :namespace_cache_enabled
55
54
 
56
- alias namespace_cache_enabled? namespace_cache_enabled
55
+ alias_method :namespace_cache_enabled?, :namespace_cache_enabled
57
56
 
58
57
  def call(record, **opts)
59
58
  chain.each do |probe|
@@ -66,22 +65,17 @@ module ActionPolicy
66
65
  private
67
66
 
68
67
  def lookup_within_namespace(policy_name, namespace, strict: false)
69
- return unless namespace
70
- NamespaceCache.fetch(namespace.name, policy_name, strict: strict) do
68
+ NamespaceCache.fetch(namespace&.name || "Kernel", policy_name, strict: strict) do
71
69
  mod = namespace
72
70
  loop do
73
- policy = "#{mod.name}::#{policy_name}".safe_constantize
74
- break policy unless policy.nil?
71
+ policy = [mod&.name, policy_name].compact.join("::").safe_constantize
72
+ break policy if policy || mod.nil? || strict
73
+
75
74
  mod = mod.namespace
76
- break if mod.nil? || strict
77
75
  end
78
76
  end
79
77
  end
80
78
 
81
- def objectify_policy(policy_name, strict: false)
82
- policy_name.safe_constantize unless strict
83
- end
84
-
85
79
  def policy_class_name_for(record)
86
80
  return record.policy_name.to_s if record.respond_to?(:policy_name)
87
81
 
@@ -110,17 +104,10 @@ module ActionPolicy
110
104
  record.class.policy_class if record.class.respond_to?(:policy_class)
111
105
  }
112
106
 
113
- # Lookup within namespace when provided
114
- NAMESPACE_LOOKUP = ->(record, namespace: nil, **) {
115
- next if namespace.nil?
116
-
117
- policy_name = policy_class_name_for(record)
118
- lookup_within_namespace(policy_name, namespace)
119
- }
120
-
121
107
  # Infer from class name
122
- INFER_FROM_CLASS = ->(record, **) {
123
- policy_class_name_for(record).safe_constantize
108
+ INFER_FROM_CLASS = ->(record, namespace: nil, strict_namespace: false, **) {
109
+ policy_name = policy_class_name_for(record)
110
+ lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
124
111
  }
125
112
 
126
113
  # Infer from passed symbol
@@ -128,8 +115,7 @@ module ActionPolicy
128
115
  next unless record.is_a?(Symbol)
129
116
 
130
117
  policy_name = "#{record.camelize}Policy"
131
- lookup_within_namespace(policy_name, namespace, strict: strict_namespace) ||
132
- objectify_policy(policy_name, strict: strict_namespace)
118
+ lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
133
119
  }
134
120
 
135
121
  # (Optional) Infer using String#classify if available
@@ -137,8 +123,7 @@ module ActionPolicy
137
123
  next unless record.is_a?(Symbol)
138
124
 
139
125
  policy_name = "#{record.to_s.classify}Policy"
140
- lookup_within_namespace(policy_name, namespace, strict: strict_namespace) ||
141
- objectify_policy(policy_name, strict: strict_namespace)
126
+ lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
142
127
  }
143
128
 
144
129
  self.chain = [
@@ -146,7 +131,6 @@ module ActionPolicy
146
131
  (CLASSIFY_SYMBOL_LOOKUP if String.method_defined?(:classify)),
147
132
  INSTANCE_POLICY_CLASS,
148
133
  CLASS_POLICY_CLASS,
149
- NAMESPACE_LOOKUP,
150
134
  INFER_FROM_CLASS
151
135
  ].compact
152
136
  end
@@ -53,11 +53,11 @@ module ActionPolicy
53
53
  def rules_aliases
54
54
  return @rules_aliases if instance_variable_defined?(:@rules_aliases)
55
55
 
56
- if superclass.respond_to?(:rules_aliases)
56
+ @rules_aliases = if superclass.respond_to?(:rules_aliases)
57
57
  superclass.rules_aliases.dup
58
58
  else
59
59
  {}
60
- end => @rules_aliases
60
+ end
61
61
  end
62
62
 
63
63
  def method_added(name)
@@ -75,11 +75,11 @@ module ActionPolicy
75
75
  def authorization_targets
76
76
  return @authorization_targets if instance_variable_defined?(:@authorization_targets)
77
77
 
78
- if superclass.respond_to?(:authorization_targets)
78
+ @authorization_targets = if superclass.respond_to?(:authorization_targets)
79
79
  superclass.authorization_targets.dup
80
80
  else
81
81
  {}
82
- end => @authorization_targets
82
+ end
83
83
  end
84
84
  end
85
85
  end
@@ -89,11 +89,11 @@ module ActionPolicy # :nodoc:
89
89
  def cached_rules
90
90
  return @cached_rules if instance_variable_defined?(:@cached_rules)
91
91
 
92
- if superclass.respond_to?(:cached_rules)
92
+ @cached_rules = if superclass.respond_to?(:cached_rules)
93
93
  superclass.cached_rules.dup
94
94
  else
95
95
  {}
96
- end => @cached_rules
96
+ end
97
97
  end
98
98
  end
99
99
  end
@@ -150,11 +150,11 @@ module ActionPolicy
150
150
  def pre_checks
151
151
  return @pre_checks if instance_variable_defined?(:@pre_checks)
152
152
 
153
- if superclass.respond_to?(:pre_checks)
153
+ @pre_checks = if superclass.respond_to?(:pre_checks)
154
154
  superclass.pre_checks.dup
155
155
  else
156
156
  []
157
- end => @pre_checks
157
+ end
158
158
  end
159
159
  end
160
160
  end
@@ -72,7 +72,7 @@ module ActionPolicy
72
72
  def all_details
73
73
  return @all_details if defined?(@all_details)
74
74
 
75
- {}.tap do |all|
75
+ @all_details = {}.tap do |all|
76
76
  next unless defined?(@reasons)
77
77
 
78
78
  reasons.reasons.each_value do |rules|
@@ -84,7 +84,7 @@ module ActionPolicy
84
84
  all.merge!(details)
85
85
  end
86
86
  end
87
- end => @all_details
87
+ end
88
88
  end
89
89
 
90
90
  # Add reasons to inspect
@@ -185,10 +185,12 @@ module ActionPolicy
185
185
  def allowed_to?(rule, record = :__undef__, **options)
186
186
  res =
187
187
  if (record == :__undef__ || record == self.record) && options.empty?
188
+ rule = resolve_rule(rule)
188
189
  policy = self
189
190
  with_clean_result { apply(rule) }
190
191
  else
191
192
  policy = policy_for(record: record, **options)
193
+ rule = policy.resolve_rule(rule)
192
194
 
193
195
  policy.apply(rule)
194
196
  policy.result
@@ -148,11 +148,11 @@ module ActionPolicy
148
148
  def scope_matchers
149
149
  return @scope_matchers if instance_variable_defined?(:@scope_matchers)
150
150
 
151
- if superclass.respond_to?(:scope_matchers)
151
+ @scope_matchers = if superclass.respond_to?(:scope_matchers)
152
152
  superclass.scope_matchers.dup
153
153
  else
154
154
  []
155
- end => @scope_matchers
155
+ end
156
156
  end
157
157
  end
158
158
  end
@@ -21,6 +21,7 @@ module ActionPolicy
21
21
  yield scopes.first.target
22
22
  end
23
23
  end
24
+
24
25
  # Asserts that the given policy was used to authorize the given target.
25
26
  #
26
27
  # def test_authorize
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionPolicy
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_policy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-29 00:00:00.000000000 Z
11
+ date: 2020-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.10.3
19
+ version: 0.11.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.10.3
26
+ version: 0.11.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ammeter
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +132,31 @@ files:
132
132
  - CHANGELOG.md
133
133
  - LICENSE.txt
134
134
  - README.md
135
+ - config/rubocop-rspec.yml
136
+ - lib/.rbnext/1995.next/action_policy/behaviours/policy_for.rb
137
+ - lib/.rbnext/1995.next/action_policy/behaviours/scoping.rb
138
+ - lib/.rbnext/1995.next/action_policy/policy/authorization.rb
139
+ - lib/.rbnext/1995.next/action_policy/utils/pretty_print.rb
140
+ - lib/.rbnext/2.7/action_policy/behaviours/policy_for.rb
141
+ - lib/.rbnext/2.7/action_policy/i18n.rb
142
+ - lib/.rbnext/2.7/action_policy/policy/cache.rb
143
+ - lib/.rbnext/2.7/action_policy/policy/pre_check.rb
144
+ - lib/.rbnext/2.7/action_policy/rspec/be_authorized_to.rb
145
+ - lib/.rbnext/2.7/action_policy/rspec/have_authorized_scope.rb
146
+ - lib/.rbnext/2.7/action_policy/utils/pretty_print.rb
147
+ - lib/.rbnext/3.0/action_policy/behaviours/thread_memoized.rb
148
+ - lib/.rbnext/3.0/action_policy/ext/policy_cache_key.rb
149
+ - lib/.rbnext/3.0/action_policy/policy/aliases.rb
150
+ - lib/.rbnext/3.0/action_policy/policy/cache.rb
151
+ - lib/.rbnext/3.0/action_policy/policy/core.rb
152
+ - lib/.rbnext/3.0/action_policy/policy/defaults.rb
153
+ - lib/.rbnext/3.0/action_policy/policy/execution_result.rb
154
+ - lib/.rbnext/3.0/action_policy/policy/pre_check.rb
155
+ - lib/.rbnext/3.0/action_policy/policy/reasons.rb
156
+ - lib/.rbnext/3.0/action_policy/rspec/be_authorized_to.rb
157
+ - lib/.rbnext/3.0/action_policy/rspec/have_authorized_scope.rb
158
+ - lib/.rbnext/3.0/action_policy/utils/pretty_print.rb
159
+ - lib/.rbnext/3.0/action_policy/utils/suggest_message.rb
135
160
  - lib/action_policy.rb
136
161
  - lib/action_policy/authorizer.rb
137
162
  - lib/action_policy/base.rb