Class: Pundit::Matchers::Utils::PolicyInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/pundit/matchers/utils/policy_info.rb

Overview

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.

Constant Summary collapse

USER_NOT_IMPLEMENTED_ERROR =

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

<<~MSG
  '%<policy>s' does not implement '%<user_alias>s'. You may want to
  configure an alias, which you can do as follows:

  Pundit::Matchers.configure do |config|
    # Alias for all policies
    config.default_user_alias = :%<user_alias>s

    # Per-policy alias
    config.user_aliases = { '%<policy>s' => :%<user_alias>s }
  end
MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ PolicyInfo

Initializes a new instance of PolicyInfo.

Parameters:

  • policy (Class)

    The policy class to collect details about.



30
31
32
33
# File 'lib/pundit/matchers/utils/policy_info.rb', line 30

def initialize(policy)
  @policy = policy
  check_user_alias!
end

Instance Attribute Details

#policyObject (readonly)

Returns the value of attribute policy.



25
26
27
# File 'lib/pundit/matchers/utils/policy_info.rb', line 25

def policy
  @policy
end

Instance Method Details

#actionsArray<Symbol>

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.

Returns:

  • (Array<Symbol>)

    An array of all actions defined in the policy class.



54
55
56
57
58
# 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_actionsArray<Symbol>

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

Returns:

  • (Array<Symbol>)

    An array of all forbidden actions defined in the policy class.



70
71
72
# File 'lib/pundit/matchers/utils/policy_info.rb', line 70

def forbidden_actions
  @forbidden_actions ||= actions - permitted_actions
end

#permitted_actionsArray<Symbol>

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

Returns:

  • (Array<Symbol>)

    An array of all permitted actions defined in the policy class.



63
64
65
# File 'lib/pundit/matchers/utils/policy_info.rb', line 63

def permitted_actions
  @permitted_actions ||= actions.select { |action| policy.public_send(:"#{action}?") }
end

#to_sString

Returns a string representation of the policy class name.

Returns:

  • (String)

    A string representation of the policy class name.



38
39
40
# File 'lib/pundit/matchers/utils/policy_info.rb', line 38

def to_s
  policy.class.name
end

#userObject

Returns the user object associated with the policy.

Returns:

  • (Object)

    The user object associated with the policy.



45
46
47
# File 'lib/pundit/matchers/utils/policy_info.rb', line 45

def user
  @user ||= policy.public_send(user_alias)
end