class Pundit::Matchers::Utils::PolicyInfo

This class provides methods to retrieve information about a policy class, such as the actions it defines and which of those actions are permitted or forbidden. It also provides a string representation of the policy class name and the user object associated with the policy.

Constants

USER_NOT_IMPLEMENTED_ERROR

Error message when policy does not respond to ‘user_alias`.

Attributes

policy[R]

Public Class Methods

new(policy) click to toggle source

Initializes a new instance of PolicyInfo.

@param policy [Class] The policy class to collect details about.

# File lib/pundit/matchers/utils/policy_info.rb, line 30
def initialize(policy)
  @policy = policy
  check_user_alias!
end

Public Instance Methods

actions() click to toggle source

Returns an array of all actions defined in the policy class.

It assumes that actions are defined as public instance methods that end with a question mark.

@return [Array<Symbol>] An array of all actions defined in the policy class.

# File lib/pundit/matchers/utils/policy_info.rb, line 54
def actions
  @actions ||= policy_public_methods.grep(/\?$/).sort.map do |policy_method|
    policy_method.to_s.delete_suffix('?').to_sym
  end
end
forbidden_actions() click to toggle source

Returns an array of all forbidden actions defined in the policy class.

@return [Array<Symbol>] An array of all forbidden actions defined in the policy class.

# File lib/pundit/matchers/utils/policy_info.rb, line 70
def forbidden_actions
  @forbidden_actions ||= actions - permitted_actions
end
permitted_actions() click to toggle source

Returns an array of all permitted actions defined in the policy class.

@return [Array<Symbol>] An array of all permitted actions defined in the policy class.

# File lib/pundit/matchers/utils/policy_info.rb, line 63
def permitted_actions
  @permitted_actions ||= actions.select { |action| policy.public_send(:"#{action}?") }
end
to_s() click to toggle source

Returns a string representation of the policy class name.

@return [String] A string representation of the policy class name.

# File lib/pundit/matchers/utils/policy_info.rb, line 38
def to_s
  policy.class.name
end
user() click to toggle source

Returns the user object associated with the policy.

@return [Object] The user object associated with the policy.

# File lib/pundit/matchers/utils/policy_info.rb, line 45
def user
  @user ||= policy.public_send(user_alias)
end

Private Instance Methods

check_user_alias!() click to toggle source
# File lib/pundit/matchers/utils/policy_info.rb, line 84
def check_user_alias!
  return if policy.respond_to?(user_alias)

  raise ArgumentError, format(USER_NOT_IMPLEMENTED_ERROR, policy: self, user_alias: user_alias)
end
policy_public_methods() click to toggle source
# File lib/pundit/matchers/utils/policy_info.rb, line 76
def policy_public_methods
  @policy_public_methods ||= policy.public_methods - Object.instance_methods
end
user_alias() click to toggle source
# File lib/pundit/matchers/utils/policy_info.rb, line 80
def user_alias
  @user_alias ||= Pundit::Matchers.configuration.user_alias(policy)
end