Module: Pundit::Matchers

Defined in:
lib/pundit/matchers.rb,
lib/pundit/matchers/base_matcher.rb,
lib/pundit/matchers/actions_matcher.rb,
lib/pundit/matchers/utils/policy_info.rb,
lib/pundit/matchers/attributes_matcher.rb,
lib/pundit/matchers/permit_actions_matcher.rb,
lib/pundit/matchers/permit_attributes_matcher.rb,
lib/pundit/matchers/forbid_all_actions_matcher.rb,
lib/pundit/matchers/permit_all_actions_matcher.rb,
lib/pundit/matchers/forbid_only_actions_matcher.rb,
lib/pundit/matchers/permit_only_actions_matcher.rb

Overview

Matchers module provides a set of RSpec matchers for testing Pundit policies.

Defined Under Namespace

Modules: Utils Classes: ActionsMatcher, AttributesMatcher, BaseMatcher, Configuration, ForbidAllActionsMatcher, ForbidOnlyActionsMatcher, PermitActionsMatcher, PermitAllActionsMatcher, PermitAttributesMatcher, PermitOnlyActionsMatcher

Constant Summary collapse

NEGATED_DESCRIPTION =

A Proc that negates the description of a matcher.

->(description) { description.gsub(/^permit/, 'forbid') }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationConfiguration

Returns the configuration object for Pundit Matchers.

Returns:



62
63
64
# File 'lib/pundit/matchers.rb', line 62

def configuration
  @configuration ||= Pundit::Matchers::Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Configures Pundit Matchers.

Yield Parameters:

  • configuration (Configuration)

    the configuration object to be modified.



55
56
57
# File 'lib/pundit/matchers.rb', line 55

def configure
  yield(configuration)
end

Instance Method Details

#forbid_actionObject

The negated matcher of #permit_action.

Same as expect(policy).not_to permit_action(*args).



81
# File 'lib/pundit/matchers.rb', line 81

RSpec::Matchers.define_negated_matcher :forbid_action, :permit_action, &NEGATED_DESCRIPTION

#forbid_actionsObject

The negated matcher of #permit_actions.

Same as expect(policy).not_to permit_actions(*args).



91
# File 'lib/pundit/matchers.rb', line 91

RSpec::Matchers.define_negated_matcher :forbid_actions, :permit_actions, &NEGATED_DESCRIPTION

#forbid_all_actionsForbidAllActionsMatcher

Note:

The negative form not_to forbid_all_actions is not supported since it creates ambiguity. Instead use to permit_all_actions.

Creates a matcher that tests if the policy forbids all actions.

Returns:



109
110
111
# File 'lib/pundit/matchers.rb', line 109

def forbid_all_actions
  ForbidAllActionsMatcher.new
end

#forbid_attributeObject

The negated matcher of #permit_attribute.

Same as expect(policy).not_to permit_attribute(*args).



163
# File 'lib/pundit/matchers.rb', line 163

RSpec::Matchers.define_negated_matcher :forbid_attribute, :permit_attribute, &NEGATED_DESCRIPTION

#forbid_attributesObject

The negated matcher of #permit_attributes.

Same as expect(policy).not_to permit_attributes(*args).



173
# File 'lib/pundit/matchers.rb', line 173

RSpec::Matchers.define_negated_matcher :forbid_attributes, :permit_attributes, &NEGATED_DESCRIPTION

#forbid_edit_and_update_actionsObject

The negated matcher of #permit_edit_and_update_actions.

Same as expect(policy).not_to permit_edit_and_update_actions(*args).



120
121
# File 'lib/pundit/matchers.rb', line 120

RSpec::Matchers.define_negated_matcher :forbid_edit_and_update_actions, :permit_edit_and_update_actions,
&NEGATED_DESCRIPTION

#forbid_mass_assignment_ofObject

An alias matcher for #forbid_attributes.



180
# File 'lib/pundit/matchers.rb', line 180

RSpec::Matchers.alias_matcher :forbid_mass_assignment_of, :forbid_attributes

#forbid_new_and_create_actionsObject

The negated matcher of #permit_new_and_create_actions.

Same as expect(policy).not_to permit_new_and_create_actions(*args).



130
131
# File 'lib/pundit/matchers.rb', line 130

RSpec::Matchers.define_negated_matcher :forbid_new_and_create_actions, :permit_new_and_create_actions,
&NEGATED_DESCRIPTION

#forbid_only_actions(*actions) ⇒ ForbidOnlyActionsMatcher

Note:

The negative form not_to forbid_only_actions is not supported since it creates ambiguity. Instead use to permit_only_actions.

Creates a matcher that tests if the policy forbids only a set of actions.

Parameters:

  • actions (Array<String, Symbol>)

    the actions to be tested.

Returns:



151
152
153
# File 'lib/pundit/matchers.rb', line 151

def forbid_only_actions(*actions)
  ForbidOnlyActionsMatcher.new(*actions)
end

#permit_action(action) ⇒ PermitActionsMatcher

Creates a matcher that tests if the policy permits a given action.

Parameters:

  • action (String|Symbol)

    the action to be tested.

Returns:



71
72
73
# File 'lib/pundit/matchers.rb', line 71

def permit_action(action)
  PermitActionsMatcher.new(action).ensure_single_action!
end

#permit_actions(*actions) ⇒ PermitActionsMatcher

Creates a matcher that tests if the policy permits a set of actions.

Parameters:

  • actions (Array<String, Symbol>)

    the actions to be tested.

Returns:



87
88
89
# File 'lib/pundit/matchers.rb', line 87

def permit_actions(*actions)
  PermitActionsMatcher.new(*actions)
end

#permit_all_actionsPermitAllActionsMatcher

Note:

The negative form not_to permit_all_actions is not supported since it creates ambiguity. Instead use to forbid_all_actions.

Creates a matcher that tests if the policy permits all actions.

Returns:



99
100
101
# File 'lib/pundit/matchers.rb', line 99

def permit_all_actions
  PermitAllActionsMatcher.new
end

#permit_attribute(attribute) ⇒ PermitAttributesMatcher

Creates a matcher that tests if the policy permits mass assignment of an attribute.

Parameters:

  • attribute (String, Symbol, Hash)

    the attribute to be tested.

Returns:



159
160
161
# File 'lib/pundit/matchers.rb', line 159

def permit_attribute(attribute)
  PermitAttributesMatcher.new(attribute).ensure_single_attribute!
end

#permit_attributes(*attributes) ⇒ PermitAttributesMatcher

Creates a matcher that tests if the policy permits mass assignment of a set of attributes.

Parameters:

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

    the attributes to be tested.

Returns:



169
170
171
# File 'lib/pundit/matchers.rb', line 169

def permit_attributes(*attributes)
  PermitAttributesMatcher.new(*attributes)
end

#permit_edit_and_update_actionsPermitActionsMatcher

Creates a matcher that tests if the policy permits the edit and update actions.

Returns:



116
117
118
# File 'lib/pundit/matchers.rb', line 116

def permit_edit_and_update_actions
  PermitActionsMatcher.new(:edit, :update)
end

#permit_mass_assignment_ofObject

An alias matcher for #permit_attributes.



179
# File 'lib/pundit/matchers.rb', line 179

RSpec::Matchers.alias_matcher :permit_mass_assignment_of, :permit_attributes

#permit_new_and_create_actionsPermitActionsMatcher

Creates a matcher that tests if the policy permits the new and create actions.

Returns:



126
127
128
# File 'lib/pundit/matchers.rb', line 126

def permit_new_and_create_actions
  PermitActionsMatcher.new(:new, :create)
end

#permit_only_actions(*actions) ⇒ PermitOnlyActionsMatcher

Note:

The negative form not_to permit_only_actions is not supported since it creates ambiguity. Instead use to forbid_only_actions.

Creates a matcher that tests if the policy permits only a set of actions.

Parameters:

  • actions (Array<String, Symbol>)

    the actions to be tested.

Returns:



140
141
142
# File 'lib/pundit/matchers.rb', line 140

def permit_only_actions(*actions)
  PermitOnlyActionsMatcher.new(*actions)
end