class Pundit::Matchers::ActionsMatcher

This is the base action matcher class. Matchers related to actions should inherit from this class.

Constants

ACTIONS_NOT_IMPLEMENTED_ERROR

Error message when actions are not implemented in a policy.

ARGUMENTS_REQUIRED_ERROR

Error message when at least one action must be specified.

ONE_ARGUMENT_REQUIRED_ERROR

Error message when only one action may be specified.

Attributes

expected_actions[R]

Public Class Methods

new(*expected_actions) click to toggle source

Initializes a new instance of the ActionsMatcher class.

@param expected_actions [Array<String, Symbol>] The expected actions to be checked.

@raise [ArgumentError] If no actions are specified.

Calls superclass method
# File lib/pundit/matchers/actions_matcher.rb, line 21
def initialize(*expected_actions)
  raise ArgumentError, ARGUMENTS_REQUIRED_ERROR if expected_actions.empty?

  super()
  @expected_actions = expected_actions.flatten.map(&:to_sym).sort
end

Public Instance Methods

ensure_single_action!() click to toggle source

Ensures that only one action is specified.

@raise [ArgumentError] If more than one action is specified.

@return [ActionsMatcher] The object itself.

# File lib/pundit/matchers/actions_matcher.rb, line 33
def ensure_single_action!
  raise ArgumentError, ONE_ARGUMENT_REQUIRED_ERROR if expected_actions.size > 1

  self
end

Private Instance Methods

check_actions!() click to toggle source
# File lib/pundit/matchers/actions_matcher.rb, line 43
def check_actions!
  non_explicit_actions = (expected_actions - policy_info.actions)
  missing_actions = non_explicit_actions.reject { |action| policy_info.policy.respond_to?(:"#{action}?") }
  return if missing_actions.empty?

  raise ArgumentError, format(
    ACTIONS_NOT_IMPLEMENTED_ERROR,
    policy: policy_info, actions: missing_actions
  )
end