Skip to content

Commit

Permalink
Regroup documentation for pundit_reset!
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgestrand committed Nov 22, 2024
1 parent bfa0de5 commit ddaa1e6
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions lib/pundit/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,33 @@ def pundit
# Hook method which allows customizing which user is passed to policies and
# scopes initialized by {#authorize}, {#policy} and {#policy_scope}.
#
# @note Make sure to call `pundit_reset!` if this changes during a request.
# @see https://github.com/varvet/pundit#customize-pundit-user
# @see #pundit
# @see #pundit_reset!
# @return [Object] the user object to be used with pundit
def pundit_user
current_user
end

# Clears the cached Pundit authorization data.
#
# This method should be called when the pundit_user is changed,
# such as during user switching, to ensure that stale authorization
# data is not used. Pundit caches authorization policies and scopes
# for the pundit_user, so calling this method will reset those
# caches and ensure that the next authorization checks are performed
# with the correct context for the new pundit_user.
#
# @return [void]
def pundit_reset!
@pundit = nil
@_pundit_policies = nil
@_pundit_policy_scopes = nil
@_pundit_policy_authorized = nil
@_pundit_policy_scoped = nil
end

# @!group Policies

# Retrieves the policy for the given record, initializing it with the record
Expand Down Expand Up @@ -217,27 +238,5 @@ def pundit_params_for(record)
end

# @!endgroup

# @!group Customize Pundit user

# Clears the cached Pundit authorization data.
#
# This method should be called when the pundit_user is changed,
# such as during user switching, to ensure that stale authorization
# data is not used. Pundit caches authorization policies and scopes
# for the pundit_user, so calling this method will reset those
# caches and ensure that the next authorization checks are performed
# with the correct context for the new pundit_user.
#
# @return [void]
def pundit_reset!
@pundit = nil
@_pundit_policies = nil
@_pundit_policy_scopes = nil
@_pundit_policy_authorized = nil
@_pundit_policy_scoped = nil
end

# @!endgroup
end
end

0 comments on commit ddaa1e6

Please sign in to comment.