class Pundit::Matchers::AttributesMatcher

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

Constants

ARGUMENTS_REQUIRED_ERROR

Error message to be raised when no attributes are specified.

ONE_ARGUMENT_REQUIRED_ERROR

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

Attributes

expected_attributes[R]
options[R]

Public Class Methods

new(*expected_attributes) click to toggle source

Initializes a new instance of the AttributesMatcher class.

@param expected_attributes [Array<String, Symbol, Hash>] The list of attributes to be tested.

Calls superclass method
# 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

Public Instance Methods

ensure_single_attribute!() click to toggle source

Ensures that only one attribute is specified.

@raise [ArgumentError] If more than one attribute is specified.

@return [AttributesMatcher] The object itself.

# 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) click to toggle source

Specifies the action to be tested.

@param action [Symbol, String] The action to be tested. @return [AttributesMatcher] The current instance of the AttributesMatcher class.

# File lib/pundit/matchers/attributes_matcher.rb, line 29
def for_action(action)
  @options[:action] = action
  self
end

Private Instance Methods

action_message() click to toggle source
# File lib/pundit/matchers/attributes_matcher.rb, line 58
def action_message
  " when authorising the '#{options[:action]}' action"
end
flatten_attributes(attributes) click to toggle source

Flattens and sorts a hash or array of attributes into an array of symbols.

This is a private method used internally by the ‘Matcher` class to convert attribute lists into a flattened, sorted array of symbols. The resulting array can be used to compare attribute lists.

@param attributes [String, Symbol, Array, Hash] the attributes to be flattened. @return [Array<Symbol>] the flattened, sorted array of symbols.

# File lib/pundit/matchers/attributes_matcher.rb, line 70
def flatten_attributes(attributes)
  case attributes
  when String, Symbol
    [attributes.to_sym]
  when Array
    attributes.flat_map { |item| flatten_attributes(item) }.sort
  when Hash
    attributes.flat_map do |key, value|
      flatten_attributes(value).map { |item| :"#{key}[#{item}]" }
    end.sort
  end
end
permitted_attributes(policy) click to toggle source
# File lib/pundit/matchers/attributes_matcher.rb, line 49
def permitted_attributes(policy)
  @permitted_attributes ||=
    if options.key?(:action)
      flatten_attributes(policy.public_send(:"permitted_attributes_for_#{options[:action]}"))
    else
      flatten_attributes(policy.permitted_attributes)
    end
end