lib/pundit/matchers.rb in pundit-matchers-2.1.0 vs lib/pundit/matchers.rb in pundit-matchers-2.2.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'rspec/core'
module Pundit
module Matchers
require_relative 'matchers/utils/policy_info'
@@ -28,31 +30,29 @@
def configuration
@configuration ||= Pundit::Matchers::Configuration.new
end
end
+ end
- RSpec::Matchers.define :forbid_action do |action, *args, **kwargs|
- match do |policy|
- if args.any?
- !policy.public_send("#{action}?", *args, **kwargs)
- else
- !policy.public_send("#{action}?", **kwargs)
- end
+ RSpec::Matchers.define :forbid_action do |action, *args, **kwargs|
+ match do |policy|
+ if args.any?
+ !policy.public_send("#{action}?", *args, **kwargs)
+ else
+ !policy.public_send("#{action}?", **kwargs)
end
+ end
- failure_message do |policy|
- "#{policy.class} does not forbid #{action} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
- end
+ failure_message do |policy|
+ "#{policy.class} does not forbid #{action} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
+ end
- failure_message_when_negated do |policy|
- "#{policy.class} does not permit #{action} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
- end
+ failure_message_when_negated do |policy|
+ "#{policy.class} does not permit #{action} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :forbid_actions do |*actions|
actions.flatten!
@@ -72,44 +72,40 @@
failure_message do |policy|
if actions.count.zero?
zero_actions_failure_message
else
- "#{policy.class} expected to forbid #{actions}, but allowed " \
- "#{allowed_actions} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} expected to forbid #{actions}, but permitted " \
+ "#{allowed_actions} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
failure_message_when_negated do |policy|
if actions.count.zero?
zero_actions_failure_message
else
"#{policy.class} expected to permit #{actions}, but forbade " \
- "#{allowed_actions} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{allowed_actions} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
end
RSpec::Matchers.define :forbid_edit_and_update_actions do
match do |policy|
!policy.edit? && !policy.update?
end
failure_message do |policy|
- "#{policy.class} does not forbid the edit or update action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not forbid the edit or update action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
failure_message_when_negated do |policy|
- "#{policy.class} does not permit the edit or update action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not permit the edit or update action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :forbid_mass_assignment_of do |attributes|
# Map single object argument to an array, if necessary
@@ -141,59 +137,53 @@
failure_message do |policy|
if attributes.count.zero?
zero_attributes_failure_message
elsif defined? @action
"#{policy.class} expected to forbid the mass assignment of the " \
- "attributes #{attributes} when authorising the #{@action} action, " \
- 'but allowed the mass assignment of the attributes ' \
- "#{allowed_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes} when authorising the #{@action} action, " \
+ 'but permitted the mass assignment of the attributes ' \
+ "#{allowed_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
else
"#{policy.class} expected to forbid the mass assignment of the " \
- "attributes #{attributes}, but allowed the mass assignment of " \
- "the attributes #{allowed_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes}, but permitted the mass assignment of " \
+ "the attributes #{allowed_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
failure_message_when_negated do |policy|
if attributes.count.zero?
zero_attributes_failure_message
elsif defined? @action
"#{policy.class} expected to permit the mass assignment of the " \
- "attributes #{attributes} when authorising the #{@action} action, " \
- 'but permitted the mass assignment of the attributes ' \
- "#{allowed_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes} when authorising the #{@action} action, " \
+ 'but permitted the mass assignment of the attributes ' \
+ "#{allowed_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
else
"#{policy.class} expected to permit the mass assignment of the " \
- "attributes #{attributes}, but permitted the mass assignment of " \
- "the attributes #{allowed_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes}, but permitted the mass assignment of " \
+ "the attributes #{allowed_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
end
RSpec::Matchers.define :forbid_new_and_create_actions do
match do |policy|
!policy.new? && !policy.create?
end
failure_message do |policy|
- "#{policy.class} does not forbid the new or create action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not forbid the new or create action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
failure_message_when_negated do |policy|
- "#{policy.class} does not permit the new or create action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not permit the new or create action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :permit_action do |action, *args, **kwargs|
match do |policy|
@@ -203,19 +193,17 @@
policy.public_send("#{action}?", **kwargs)
end
end
failure_message do |policy|
- "#{policy.class} does not permit #{action} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not permit #{action} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
failure_message_when_negated do |policy|
- "#{policy.class} does not forbid #{action} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not forbid #{action} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :permit_actions do |*actions|
actions.flatten!
@@ -257,43 +245,39 @@
failure_message do |policy|
if actions.count.zero?
zero_actions_failure_message
else
"#{policy.class} expected to permit #{actions}, but forbade " \
- "#{forbidden_actions} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{forbidden_actions} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
failure_message_when_negated do |policy|
if actions.count.zero?
zero_actions_failure_message
else
- "#{policy.class} expected to forbid #{actions}, but allowed " \
- "#{forbidden_actions} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} expected to forbid #{actions}, but permitted " \
+ "#{forbidden_actions} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
end
RSpec::Matchers.define :permit_edit_and_update_actions do
match do |policy|
policy.edit? && policy.update?
end
failure_message do |policy|
- "#{policy.class} does not permit the edit or update action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not permit the edit or update action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
failure_message_when_negated do |policy|
- "#{policy.class} does not forbid the edit or update action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not forbid the edit or update action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :permit_mass_assignment_of do |attributes|
# Map single object argument to an array, if necessary
@@ -325,58 +309,52 @@
failure_message do |policy|
if attributes.count.zero?
zero_attributes_failure_message
elsif defined? @action
"#{policy.class} expected to permit the mass assignment of the " \
- "attributes #{attributes} when authorising the #{@action} action, " \
- 'but forbade the mass assignment of the attributes ' \
- "#{forbidden_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes} when authorising the #{@action} action, " \
+ 'but forbade the mass assignment of the attributes ' \
+ "#{forbidden_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
else
"#{policy.class} expected to permit the mass assignment of the " \
- "attributes #{attributes}, but forbade the mass assignment of the " \
- "attributes #{forbidden_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes}, but forbade the mass assignment of the " \
+ "attributes #{forbidden_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
failure_message_when_negated do |policy|
if attributes.count.zero?
zero_attributes_failure_message
elsif defined? @action
"#{policy.class} expected to forbid the mass assignment of the " \
- "attributes #{attributes} when authorising the #{@action} action, " \
- 'but forbade the mass assignment of the attributes ' \
- "#{forbidden_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes} when authorising the #{@action} action, " \
+ 'but forbade the mass assignment of the attributes ' \
+ "#{forbidden_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
else
"#{policy.class} expected to forbid the mass assignment of the " \
- "attributes #{attributes}, but forbade the mass assignment of the " \
- "attributes #{forbidden_attributes} for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "attributes #{attributes}, but forbade the mass assignment of the " \
+ "attributes #{forbidden_attributes} for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
end
RSpec::Matchers.define :permit_new_and_create_actions do
match do |policy|
policy.new? && policy.create?
end
failure_message do |policy|
- "#{policy.class} does not permit the new or create action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not permit the new or create action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
failure_message_when_negated do |policy|
- "#{policy.class} does not forbid the new or create action for " +
- policy.public_send(Pundit::Matchers.configuration.user_alias)
- .inspect + '.'
+ "#{policy.class} does not forbid the new or create action for " \
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
end
end
RSpec::Matchers.define :permit_all_actions do
match do |policy|