Skip to content

Commit a457343

Browse files
authored
Merge pull request #858 from varvet/kbs/janitor-2
Update inline code documentation
2 parents f5095b4 + 432160a commit a457343

File tree

11 files changed

+149
-69
lines changed

11 files changed

+149
-69
lines changed

lib/pundit.rb

Lines changed: 10 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,30 @@
33
require "active_support"
44

55
require "pundit/version"
6+
require "pundit/error"
67
require "pundit/policy_finder"
7-
require "pundit/authorization"
88
require "pundit/context"
9+
require "pundit/authorization"
10+
require "pundit/helper"
911
require "pundit/cache_store"
1012
require "pundit/cache_store/null_store"
1113
require "pundit/cache_store/legacy_store"
1214
require "pundit/railtie" if defined?(Rails)
1315

14-
# @api private
15-
# To avoid name clashes with common Error naming when mixing in Pundit,
16-
# keep it here with compact class style definition.
17-
class Pundit::Error < StandardError; end # rubocop:disable Style/ClassAndModuleChildren
18-
1916
# Hello? Yes, this is Pundit.
2017
#
2118
# @api public
2219
module Pundit
2320
# @api private
21+
# @since v1.0.0
2422
# @deprecated See {Pundit::PolicyFinder}
2523
SUFFIX = Pundit::PolicyFinder::SUFFIX
2624

2725
# @api private
2826
# @private
27+
# @since v0.1.0
2928
module Generators; end
3029

31-
# Error that will be raised when authorization has failed
32-
class NotAuthorizedError < Error
33-
# @see #initialize
34-
attr_reader :query
35-
# @see #initialize
36-
attr_reader :record
37-
# @see #initialize
38-
attr_reader :policy
39-
40-
# @overload initialize(message)
41-
# Create an error with a simple error message.
42-
# @param [String] message A simple error message string.
43-
#
44-
# @overload initialize(options)
45-
# Create an error with the specified attributes.
46-
# @param [Hash] options The error options.
47-
# @option options [String] :message Optional custom error message. Will default to a generalized message.
48-
# @option options [Symbol] :query The name of the policy method that was checked.
49-
# @option options [Object] :record The object that was being checked with the policy.
50-
# @option options [Class] :policy The class of policy that was used for the check.
51-
def initialize(options = {})
52-
if options.is_a? String
53-
message = options
54-
else
55-
@query = options[:query]
56-
@record = options[:record]
57-
@policy = options[:policy]
58-
59-
message = options.fetch(:message) do
60-
record_name = record.is_a?(Class) ? record.to_s : "this #{record.class}"
61-
"not allowed to #{policy.class}##{query} #{record_name}"
62-
end
63-
end
64-
65-
super(message)
66-
end
67-
end
68-
69-
# Error that will be raised if a policy or policy scope constructor is not called correctly.
70-
class InvalidConstructorError < Error; end
71-
72-
# Error that will be raised if a controller action has not called the
73-
# `authorize` or `skip_authorization` methods.
74-
class AuthorizationNotPerformedError < Error; end
75-
76-
# Error that will be raised if a controller action has not called the
77-
# `policy_scope` or `skip_policy_scope` methods.
78-
class PolicyScopingNotPerformedError < AuthorizationNotPerformedError; end
79-
80-
# Error that will be raised if a policy or policy scope is not defined.
81-
class NotDefinedError < Error; end
82-
8330
def self.included(base)
8431
location = caller_locations(1, 1).first
8532
warn <<~WARNING
@@ -91,6 +38,7 @@ def self.included(base)
9138

9239
class << self
9340
# @see Pundit::Context#authorize
41+
# @since v1.0.0
9442
def authorize(user, record, query, policy_class: nil, cache: nil)
9543
context = if cache
9644
policy_cache = CacheStore::LegacyStore.new(cache)
@@ -103,34 +51,27 @@ def authorize(user, record, query, policy_class: nil, cache: nil)
10351
end
10452

10553
# @see Pundit::Context#policy_scope
54+
# @since v0.1.0
10655
def policy_scope(user, *args, **kwargs, &block)
10756
Context.new(user: user).policy_scope(*args, **kwargs, &block)
10857
end
10958

11059
# @see Pundit::Context#policy_scope!
60+
# @since v0.1.0
11161
def policy_scope!(user, *args, **kwargs, &block)
11262
Context.new(user: user).policy_scope!(*args, **kwargs, &block)
11363
end
11464

11565
# @see Pundit::Context#policy
66+
# @since v0.1.0
11667
def policy(user, *args, **kwargs, &block)
11768
Context.new(user: user).policy(*args, **kwargs, &block)
11869
end
11970

12071
# @see Pundit::Context#policy!
72+
# @since v0.1.0
12173
def policy!(user, *args, **kwargs, &block)
12274
Context.new(user: user).policy!(*args, **kwargs, &block)
12375
end
12476
end
125-
126-
# Rails view helpers, to allow a slightly different view-specific
127-
# implementation of the methods in {Pundit::Authorization}.
128-
#
129-
# @api private
130-
module Helper
131-
# @see Pundit::Authorization#pundit_policy_scope
132-
def policy_scope(scope)
133-
pundit_policy_scope(scope)
134-
end
135-
end
13677
end

lib/pundit/authorization.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Pundit
99
# end
1010
# @see #pundit
1111
# @api public
12+
# @since v2.2.0
1213
module Authorization
1314
extend ActiveSupport::Concern
1415

@@ -30,6 +31,7 @@ module Authorization
3031
# @return [Pundit::Context]
3132
# @see #pundit_user
3233
# @see #policies
34+
# @since v2.3.2
3335
def pundit
3436
@pundit ||= Pundit::Context.new(
3537
user: pundit_user,
@@ -45,6 +47,7 @@ def pundit
4547
# @see #pundit
4648
# @see #pundit_reset!
4749
# @return [Object] the user object to be used with pundit
50+
# @since v0.2.2
4851
def pundit_user
4952
current_user
5053
end
@@ -59,6 +62,7 @@ def pundit_user
5962
# with the correct context for the new pundit_user.
6063
#
6164
# @return [void]
65+
# @since v2.5.0
6266
def pundit_reset!
6367
@pundit = nil
6468
@_pundit_policies = nil
@@ -81,6 +85,7 @@ def pundit_reset!
8185
# @return [record] Always returns the passed object record
8286
# @see Pundit::Context#authorize
8387
# @see #verify_authorized
88+
# @since v0.1.0
8489
def authorize(record, query = nil, policy_class: nil)
8590
query ||= "#{action_name}?"
8691

@@ -94,13 +99,15 @@ def authorize(record, query = nil, policy_class: nil)
9499
# @see https://github.com/varvet/pundit#ensuring-policies-and-scopes-are-used
95100
# @return [void]
96101
# @see #verify_authorized
102+
# @since v1.0.0
97103
def skip_authorization
98104
@_pundit_policy_authorized = :skipped
99105
end
100106

101107
# @return [Boolean] wether or not authorization has been performed
102108
# @see #authorize
103109
# @see #skip_authorization
110+
# @since v1.0.0
104111
def pundit_policy_authorized?
105112
!!@_pundit_policy_authorized
106113
end
@@ -115,6 +122,7 @@ def pundit_policy_authorized?
115122
# @return [void]
116123
# @see #authorize
117124
# @see #skip_authorization
125+
# @since v0.1.0
118126
def verify_authorized
119127
raise AuthorizationNotPerformedError, self.class unless pundit_policy_authorized?
120128
end
@@ -124,6 +132,7 @@ def verify_authorized
124132
# Cache of policies. You should not rely on this method.
125133
#
126134
# @api private
135+
# @since v1.0.0
127136
def policies
128137
@_pundit_policies ||= {}
129138
end
@@ -137,6 +146,7 @@ def policies
137146
# @see https://github.com/varvet/pundit#policies
138147
# @param record [Object] the object we're retrieving the policy for
139148
# @return [Object] instance of policy class with query methods
149+
# @since v0.1.0
140150
def policy(record)
141151
pundit.policy!(record)
142152
end
@@ -149,6 +159,7 @@ def policy(record)
149159
# @param scope [Object] the object we're retrieving the policy scope for
150160
# @param policy_scope_class [#resolve] the policy scope class we want to force use of
151161
# @return [#resolve, nil] instance of scope class which can resolve to a scope
162+
# @since v0.1.0
152163
def policy_scope(scope, policy_scope_class: nil)
153164
@_pundit_policy_scoped = true
154165
policy_scope_class ? policy_scope_class.new(pundit_user, scope).resolve : pundit_policy_scope(scope)
@@ -159,13 +170,15 @@ def policy_scope(scope, policy_scope_class: nil)
159170
# @see https://github.com/varvet/pundit#ensuring-policies-and-scopes-are-used
160171
# @return [void]
161172
# @see #verify_policy_scoped
173+
# @since v1.0.0
162174
def skip_policy_scope
163175
@_pundit_policy_scoped = :skipped
164176
end
165177

166178
# @return [Boolean] wether or not policy scoping has been performed
167179
# @see #policy_scope
168180
# @see #skip_policy_scope
181+
# @since v1.0.0
169182
def pundit_policy_scoped?
170183
!!@_pundit_policy_scoped
171184
end
@@ -181,6 +194,7 @@ def pundit_policy_scoped?
181194
# @return [void]
182195
# @see #policy_scope
183196
# @see #skip_policy_scope
197+
# @since v0.2.1
184198
def verify_policy_scoped
185199
raise PolicyScopingNotPerformedError, self.class unless pundit_policy_scoped?
186200
end
@@ -190,6 +204,7 @@ def verify_policy_scoped
190204
# Cache of policy scope. You should not rely on this method.
191205
#
192206
# @api private
207+
# @since v1.0.0
193208
def policy_scopes
194209
@_pundit_policy_scopes ||= {}
195210
end
@@ -206,6 +221,7 @@ def policy_scopes
206221
# @note This also memoizes the instance with `scope` as the key.
207222
# @see Pundit::Helper#policy_scope
208223
# @api private
224+
# @since v1.0.0
209225
def pundit_policy_scope(scope)
210226
policy_scopes[scope] ||= pundit.policy_scope!(scope)
211227
end
@@ -228,6 +244,7 @@ def pundit_policy_scope(scope)
228244
# @param action [Symbol, String] the name of the action being performed on the record (e.g. `:update`).
229245
# If omitted then this defaults to the Rails controller action name.
230246
# @return [Hash{String => Object}] the permitted attributes
247+
# @since v1.0.0
231248
def permitted_attributes(record, action = action_name)
232249
policy = policy(record)
233250
method_name = if policy.respond_to?("permitted_attributes_for_#{action}")
@@ -242,6 +259,7 @@ def permitted_attributes(record, action = action_name)
242259
#
243260
# @param record [Object] the object we're retrieving params for
244261
# @return [ActionController::Parameters] the params
262+
# @since v2.0.0
245263
def pundit_params_for(record)
246264
params.require(PolicyFinder.new(record).param_key)
247265
end

lib/pundit/cache_store.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ module Pundit
55
#
66
# Cache stores are used to cache policy lookups, so you get the same policy
77
# instance for the same record.
8+
# @since v2.3.2
89
module CacheStore
910
# @!group Cache Store Interface
1011

1112
# @!method fetch(user:, record:, &block)
1213
# Looks up a stored policy or generate a new one.
1314
#
15+
# @since v2.3.2
1416
# @note This is a method template, but the method does not exist in this module.
1517
# @param user [Object] the user that initiated the action
1618
# @param record [Object] the object being accessed

lib/pundit/cache_store/legacy_store.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ module CacheStore
77
# The original cache mechanism used by Pundit.
88
#
99
# @api private
10+
# @since v2.3.2
1011
class LegacyStore
12+
# @since v2.3.2
1113
def initialize(hash = {})
1214
@store = hash
1315
end
1416

1517
# A cache store that uses only the record as a cache key, and ignores the user.
1618
#
1719
# @note `nil` results are not cached.
20+
# @since v2.3.2
1821
def fetch(user:, record:)
1922
_ = user
2023
@store[record] ||= yield

lib/pundit/cache_store/null_store.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ module CacheStore
88
#
99
# @see Pundit::Context#initialize
1010
# @api private
11+
# @since v2.3.2
1112
class NullStore
1213
@instance = new
1314

1415
class << self
16+
# @since v2.3.2
1517
# @return [NullStore] the singleton instance
1618
attr_reader :instance
1719
end
1820

1921
# Always yields, does not cache anything.
2022
# @yield
2123
# @return [any] whatever the block returns.
24+
# @since v2.3.2
2225
def fetch(*, **)
2326
yield
2427
end

0 commit comments

Comments
 (0)