Class: Pundit::Matchers::AttributesMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/pundit/matchers/attributes_matcher.rb

Overview

The AttributesMatcher class is used to test whether a Pundit policy allows or denies access to certain attributes.

Direct Known Subclasses

PermitAttributesMatcher

Constant Summary collapse

ARGUMENTS_REQUIRED_ERROR =

Error message to be raised when no attributes are specified.

'At least one attribute must be specified'
ONE_ARGUMENT_REQUIRED_ERROR =

Error message to be raised when only one attribute may be specified.

'Only one attribute may be specified'

Constants inherited from BaseMatcher

BaseMatcher::AMBIGUOUS_NEGATED_MATCHER_ERROR

Instance Method Summary collapse

Constructor Details

#initialize(*expected_attributes) ⇒ AttributesMatcher

Initializes a new instance of the AttributesMatcher class.

Parameters:

  • expected_attributes (Array<String, Symbol, Hash>)

    The list of attributes to be tested.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/pundit/matchers/attributes_matcher.rb', line 17

def initialize(*expected_attributes)
  raise ArgumentError, ARGUMENTS_REQUIRED_ERROR if expected_attributes.empty?

  super()
  @expected_attributes = flatten_attributes(expected_attributes)
  @options = {}
end

Instance Method Details

#ensure_single_attribute!AttributesMatcher

Ensures that only one attribute is specified.

Returns:

Raises:

  • (ArgumentError)

    If more than one attribute is specified.



39
40
41
42
43
# File 'lib/pundit/matchers/attributes_matcher.rb', line 39

def ensure_single_attribute!
  raise ArgumentError, ONE_ARGUMENT_REQUIRED_ERROR if expected_attributes.size > 1

  self
end

#for_action(action) ⇒ AttributesMatcher

Specifies the action to be tested.

Parameters:

  • action (Symbol, String)

    The action to be tested.

Returns:



29
30
31
32
# File 'lib/pundit/matchers/attributes_matcher.rb', line 29

def for_action(action)
  @options[:action] = action
  self
end