Class: Pundit::Matchers::PermitOnlyActionsMatcher

Inherits:
ActionsMatcher show all
Defined in:
lib/pundit/matchers/permit_only_actions_matcher.rb

Overview

This matcher tests whether a policy permits only the expected actions.

Constant Summary

Constants inherited from ActionsMatcher

ActionsMatcher::ACTIONS_NOT_IMPLEMENTED_ERROR, ActionsMatcher::ARGUMENTS_REQUIRED_ERROR, ActionsMatcher::ONE_ARGUMENT_REQUIRED_ERROR

Constants inherited from BaseMatcher

BaseMatcher::AMBIGUOUS_NEGATED_MATCHER_ERROR

Instance Method Summary collapse

Methods inherited from ActionsMatcher

#ensure_single_action!, #initialize

Constructor Details

This class inherits a constructor from Pundit::Matchers::ActionsMatcher

Instance Method Details

#descriptionString

A description of the matcher.

Returns:

  • (String)

    A description of the matcher.



12
13
14
# File 'lib/pundit/matchers/permit_only_actions_matcher.rb', line 12

def description
  "permit only #{expected_actions}"
end

#does_not_match?(_policy) ⇒ void

This method returns an undefined value.

Raises a NotImplementedError

Raises:

  • NotImplementedError



33
34
35
# File 'lib/pundit/matchers/permit_only_actions_matcher.rb', line 33

def does_not_match?(_policy)
  raise NotImplementedError, format(AMBIGUOUS_NEGATED_MATCHER_ERROR, name: 'permit_only_actions')
end

#failure_messageString

The failure message when the expected actions and the permitted actions do not match.

Returns:

  • (String)

    A failure message when the expected actions and the permitted actions do not match.



40
41
42
43
44
45
# File 'lib/pundit/matchers/permit_only_actions_matcher.rb', line 40

def failure_message
  message = +"expected '#{policy_info}' to permit only #{expected_actions},"
  message << " but permitted #{actual_actions}" unless actual_actions.empty?
  message << extra_message unless extra_actions.empty?
  message << user_message
end

#matches?(policy) ⇒ Boolean

Checks if the given policy permits only the expected actions.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

    True if the policy permits only the expected actions, false otherwise.



20
21
22
23
24
25
26
27
28
# File 'lib/pundit/matchers/permit_only_actions_matcher.rb', line 20

def matches?(policy)
  setup_policy_info! policy
  check_actions!

  @actual_actions = policy_info.permitted_actions - expected_actions
  @extra_actions = policy_info.forbidden_actions & expected_actions

  actual_actions.empty? && extra_actions.empty?
end