Class: Pundit::Matchers::ForbidOnlyActionsMatcher

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

Overview

This matcher tests whether a policy forbids 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)

    Description of the matcher.



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

def description
  "forbid 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/forbid_only_actions_matcher.rb', line 33

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

#failure_messageString

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

Returns:

  • (String)

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



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

def failure_message
  message = "expected '#{policy_info}' to forbid only #{expected_actions},"
  message << " but forbade #{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 forbids only the expected actions.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

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



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

def matches?(policy)
  setup_policy_info! policy
  check_actions!

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

  actual_actions.empty? && extra_actions.empty?
end