Class: Pundit::Matchers::Utils::PolicyInfo
- Inherits:
-
Object
- Object
- Pundit::Matchers::Utils::PolicyInfo
- 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
-
#policy ⇒ Object
readonly
Returns the value of attribute policy.
Instance Method Summary collapse
-
#actions ⇒ Array<Symbol>
Returns an array of all actions defined in the policy class.
-
#forbidden_actions ⇒ Array<Symbol>
Returns an array of all forbidden actions defined in the policy class.
-
#initialize(policy) ⇒ PolicyInfo
constructor
Initializes a new instance of PolicyInfo.
-
#permitted_actions ⇒ Array<Symbol>
Returns an array of all permitted actions defined in the policy class.
-
#to_s ⇒ String
Returns a string representation of the policy class name.
-
#user ⇒ Object
Returns the user object associated with the policy.
Constructor Details
#initialize(policy) ⇒ PolicyInfo
Initializes a new instance of PolicyInfo.
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
#policy ⇒ Object (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
#actions ⇒ Array<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.
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_actions ⇒ Array<Symbol>
Returns 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_actions ⇒ Array<Symbol>
Returns 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_s ⇒ String
Returns 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 |
#user ⇒ Object
Returns 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 |