lib/pundit/matchers.rb in pundit-matchers-1.4.1 vs lib/pundit/matchers.rb in pundit-matchers-1.5.0

- old
+ new

@@ -1,26 +1,39 @@ require 'rspec/core' +require 'pundit/matchers/configuration' module Pundit module Matchers + class << self + def configure + yield(configuration) + end + + def configuration + @configuration ||= Pundit::Matchers::Configuration.new + end + end + RSpec::Matchers.define :forbid_action do |action, *args| match do |policy| if args.any? !policy.public_send("#{action}?", *args) else !policy.public_send("#{action}?") end end failure_message do |policy| - "#{policy.class} does not forbid #{action} for " \ - "#{policy.user.inspect}." + "#{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.user.inspect}." + "#{policy.class} does not permit #{action} for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end end RSpec::Matchers.define :forbid_actions do |actions| @@ -36,41 +49,47 @@ zero_actions_failure_message = 'At least one action must be ' \ 'specified when using the forbid_actions matcher.' failure_message do |policy| - if actions.count == 0 + if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to forbid #{actions}, but allowed " \ - "#{allowed_actions} for #{policy.user.inspect}." + "#{allowed_actions} for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end failure_message_when_negated do |policy| - if actions.count == 0 + if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to permit #{actions}, but forbade " \ - "#{allowed_actions} for #{policy.user.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.user.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.user.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 |attribute| match do |policy| @@ -87,42 +106,52 @@ failure_message do |policy| if defined? @action "#{policy.class} does not forbid the mass assignment of the " \ "#{attribute} attribute, when authorising the #{@action} action, " \ - "for #{policy.user.inspect}." + 'for ' + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' else "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute for #{policy.user.inspect}." + "#{attribute} attribute for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end failure_message_when_negated do |policy| if defined? @action "#{policy.class} does not permit the mass assignment of the " \ "#{attribute} attribute, when authorising the #{@action} action, " \ - "for #{policy.user.inspect}." + 'for ' + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' else "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute for #{policy.user.inspect}." + "#{attribute} attribute 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.user.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.user.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| match do |policy| @@ -132,17 +161,19 @@ policy.public_send("#{action}?") end end failure_message do |policy| - "#{policy.class} does not permit #{action} for " \ - "#{policy.user.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.user.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| match do |policy| @@ -157,41 +188,47 @@ zero_actions_failure_message = 'At least one action must be specified ' \ 'when using the permit_actions matcher.' failure_message do |policy| - if actions.count == 0 + if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to permit #{actions}, but forbade " \ - "#{forbidden_actions} for #{policy.user.inspect}." + "#{forbidden_actions} for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end failure_message_when_negated do |policy| - if actions.count == 0 + if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to forbid #{actions}, but allowed " \ - "#{forbidden_actions} for #{policy.user.inspect}." + "#{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.user.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.user.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 |attribute| match do |policy| @@ -208,41 +245,51 @@ failure_message do |policy| if defined? @action "#{policy.class} does not permit the mass assignment of the " \ "#{attribute} attribute, when authorising the #{@action} action, " \ - "for #{policy.user.inspect}." + 'for ' + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' else "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute for #{policy.user.inspect}." + "#{attribute} attribute for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end failure_message_when_negated do |policy| if defined? @action "#{policy.class} does not forbid the mass assignment of the " \ "#{attribute} attribute, when authorising the #{@action} action, " \ - "for #{policy.user.inspect}." + 'for ' + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' else "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute for #{policy.user.inspect}." + "#{attribute} attribute 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.user.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.user.inspect}." + "#{policy.class} does not forbid the new or create action for " + + policy.public_send(Pundit::Matchers.configuration.user_alias) + .inspect + '.' end end end if defined?(Pundit)