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'

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)


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

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

#for_action(action) ⇒ AttributesMatcher

Specifies the action to be tested.

Parameters:

  • action (Symbol, String)

    The action to be tested.

Returns:



27
28
29
30
# File 'lib/pundit/matchers/attributes_matcher.rb', line 27

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