Class: Pundit::Matchers::PermitAttributesMatcher

Inherits:
AttributesMatcher show all
Defined in:
lib/pundit/matchers/permit_attributes_matcher.rb

Overview

This matcher tests whether a policy permits or forbids the mass assignment of the expected attributes.

Constant Summary

Constants inherited from AttributesMatcher

AttributesMatcher::ARGUMENTS_REQUIRED_ERROR, AttributesMatcher::ONE_ARGUMENT_REQUIRED_ERROR

Constants inherited from BaseMatcher

BaseMatcher::AMBIGUOUS_NEGATED_MATCHER_ERROR

Instance Method Summary collapse

Methods inherited from AttributesMatcher

#ensure_single_attribute!, #for_action, #initialize

Constructor Details

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

Instance Method Details

#descriptionString

A description of the matcher.

Returns:

  • (String)

    A description of the matcher.



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

def description
  "permit the mass assignment of #{expected_attributes}"
end

#does_not_match?(policy) ⇒ Boolean

Checks if the given policy forbids the mass assignment of the expected attributes.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

    True if the policy forbids the mass assignment of the expected attributes, false otherwise.



32
33
34
35
36
37
38
# File 'lib/pundit/matchers/permit_attributes_matcher.rb', line 32

def does_not_match?(policy)
  setup_policy_info! policy

  @actual_attributes = expected_attributes & permitted_attributes(policy)

  actual_attributes.empty?
end

#failure_messageString

The failure message when the expected attributes are forbidden.

Returns:

  • (String)

    A failure message when the expected attributes are not permitted.



43
44
45
46
47
48
# File 'lib/pundit/matchers/permit_attributes_matcher.rb', line 43

def failure_message
  message = "expected '#{policy_info}' to permit the mass assignment of #{expected_attributes}"
  message << action_message if options.key?(:action)
  message << ", but forbade the mass assignment of #{actual_attributes}"
  message << user_message
end

#failure_message_when_negatedString

The failure message when the expected attributes are permitted.

Returns:

  • (String)

    A failure message when the expected attributes are forbidden.



53
54
55
56
57
58
# File 'lib/pundit/matchers/permit_attributes_matcher.rb', line 53

def failure_message_when_negated
  message = "expected '#{policy_info}' to forbid the mass assignment of #{expected_attributes}"
  message << action_message if options.key?(:action)
  message << ", but permitted the mass assignment of #{actual_attributes}"
  message << user_message
end

#matches?(policy) ⇒ Boolean

Checks if the given policy permits the mass assignment of the expected attributes.

Parameters:

  • policy (Object)

    The policy to test.

Returns:

  • (Boolean)

    True if the policy permits the mass assignment of the expected attributes, false otherwise.



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

def matches?(policy)
  setup_policy_info! policy

  @actual_attributes = expected_attributes - permitted_attributes(policy)

  actual_attributes.empty?
end