class Pundit::Context
Attributes
@api private
Public Class Methods
# File lib/pundit/context.rb, line 5 def initialize(user:, policy_cache: CacheStore::NullStore.instance) @user = user @policy_cache = policy_cache end
Public Instance Methods
Retrieves the policy for the given record.
@see github.com/varvet/pundit#policies @param user [Object] the user that initiated the action @param record [Object] the object we’re retrieving the policy for @raise [InvalidConstructorError] if the policy constructor called incorrectly @return [Object, nil] instance of policy class with query methods
# File lib/pundit/context.rb, line 86 def policy(record) cached_find(record, &:policy) end
Retrieves the policy for the given record. Raises if not found.
@see github.com/varvet/pundit#policies @param user [Object] the user that initiated the action @param record [Object] the object we’re retrieving the policy for @raise [NotDefinedError] if the policy cannot be found @raise [InvalidConstructorError] if the policy constructor called incorrectly @return [Object] instance of policy class with query methods
# File lib/pundit/context.rb, line 98 def policy!(record) cached_find(record, &:policy!) end
Retrieves the policy scope for the given record.
@see github.com/varvet/pundit#scopes @param user [Object] the user that initiated the action @param scope [Object] the object we’re retrieving the policy scope for @raise [InvalidConstructorError] if the policy constructor called incorrectly @return [Scope{#resolve}, nil] instance of scope class which can resolve to a scope
# File lib/pundit/context.rb, line 45 def policy_scope(scope) policy_scope_class = policy_finder(scope).scope return unless policy_scope_class begin policy_scope = policy_scope_class.new(user, pundit_model(scope)) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy_scope_class}> constructor is called" end policy_scope.resolve end
Retrieves the policy scope for the given record. Raises if not found.
@see github.com/varvet/pundit#scopes @param user [Object] the user that initiated the action @param scope [Object] the object we’re retrieving the policy scope for @raise [NotDefinedError] if the policy scope cannot be found @raise [InvalidConstructorError] if the policy constructor called incorrectly @return [Scope{#resolve}] instance of scope class which can resolve to a scope
# File lib/pundit/context.rb, line 66 def policy_scope!(scope) policy_scope_class = policy_finder(scope).scope! return unless policy_scope_class begin policy_scope = policy_scope_class.new(user, pundit_model(scope)) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy_scope_class}> constructor is called" end policy_scope.resolve end
Private Instance Methods
# File lib/pundit/context.rb, line 104 def cached_find(record) policy_cache.fetch(user: user, record: record) do klass = yield policy_finder(record) next unless klass model = pundit_model(record) begin klass.new(user, model) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{klass}> constructor is called" end end end
# File lib/pundit/context.rb, line 119 def policy_finder(record) PolicyFinder.new(record) end
# File lib/pundit/context.rb, line 123 def pundit_model(record) record.is_a?(Array) ? record.last : record end