Class: Pundit::Matchers::PermitActionsMatcher

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

Overview

This matcher tests whether a policy permits or forbids 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/permit_actions_matcher.rb', line 12

def description
  "permit #{expected_actions}"
end

#does_not_match?(policy) ⇒ Boolean

Checks if the given policy forbids the expected actions.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/pundit/matchers/permit_actions_matcher.rb', line 35

def does_not_match?(policy)
  setup_policy_info! policy
  check_actions!

  @actual_actions = expected_actions.select do |action|
    policy.public_send(:"#{action}?")
  end

  actual_actions.empty?
end

#failure_messageString

Returns a failure message when the expected actions are forbidden.

Returns:

  • (String)

    A failure message when the expected actions are not forbidden.



49
50
51
52
53
# File 'lib/pundit/matchers/permit_actions_matcher.rb', line 49

def failure_message
  message = "expected '#{policy_info}' to permit #{expected_actions},"
  message << " but forbade #{actual_actions}"
  message << user_message
end

#failure_message_when_negatedString

Returns a failure message when the expected actions are permitted.

Returns:

  • (String)

    A failure message when the expected actions are permitted.



58
59
60
61
62
# File 'lib/pundit/matchers/permit_actions_matcher.rb', line 58

def failure_message_when_negated
  message = "expected '#{policy_info}' to forbid #{expected_actions},"
  message << " but permitted #{actual_actions}"
  message << user_message
end

#matches?(policy) ⇒ Boolean

Checks if the given policy permits the expected actions.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

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



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

def matches?(policy)
  setup_policy_info! policy
  check_actions!

  @actual_actions = expected_actions.reject do |action|
    policy.public_send(:"#{action}?")
  end

  actual_actions.empty?
end