lib/pundit/matchers.rb in pundit-matchers-1.3.1 vs lib/pundit/matchers.rb in pundit-matchers-1.4.0

- old
+ new

@@ -24,44 +24,38 @@ end RSpec::Matchers.define :forbid_actions do |actions| match do |policy| return false if actions.count < 2 - actions.each do |action| - return false if policy.public_send("#{action}?") + @allowed_actions = actions.select do |action| + policy.public_send("#{action}?") end - true + @allowed_actions.empty? end + attr_reader :allowed_actions + zero_actions_failure_message = 'At least two actions must be ' \ 'specified when using the forbid_actions matcher.' - one_action_failure_message = 'More than one action must be specified ' \ - 'when using the forbid_actions matcher. To test a single action, use ' \ - 'forbid_action instead.' - failure_message do |policy| case actions.count when 0 zero_actions_failure_message - when 1 - one_action_failure_message else - "#{policy.class} does not forbid #{actions} for " \ - "#{policy.user.inspect}." + "#{policy.class} expected to forbid #{actions}, but allowed " \ + "#{allowed_actions} for #{policy.user.inspect}." end end failure_message_when_negated do |policy| case actions.count when 0 zero_actions_failure_message - when 1 - one_action_failure_message else - "#{policy.class} does not permit #{actions} for " \ - "#{policy.user.inspect}." + "#{policy.class} expected to permit #{actions}, but forbade " \ + "#{allowed_actions} for #{policy.user.inspect}." end end end RSpec::Matchers.define :forbid_edit_and_update_actions do @@ -153,43 +147,39 @@ end RSpec::Matchers.define :permit_actions do |actions| match do |policy| return false if actions.count < 2 - actions.each do |action| - return false unless policy.public_send("#{action}?") + @forbidden_actions = actions.reject do |action| + policy.public_send("#{action}?") end - true + @forbidden_actions.empty? end + attr_reader :forbidden_actions + zero_actions_failure_message = 'At least two actions must be ' \ 'specified when using the permit_actions matcher.' - one_action_failure_message = 'More than one action must be specified ' \ - 'when using the permit_actions matcher. To test a single action, use ' \ - 'permit_action instead.' - failure_message do |policy| case actions.count when 0 zero_actions_failure_message - when 1 - one_action_failure_message else - "#{policy.class} does not permit #{actions} for " \ - "#{policy.user.inspect}." + "#{policy.class} expected to permit #{actions}, but forbade " \ + "#{forbidden_actions} for #{policy.user.inspect}." end end failure_message_when_negated do |policy| case actions.count when 0 zero_actions_failure_message when 1 one_action_failure_message else - "#{policy.class} does not forbid #{actions} for " \ - "#{policy.user.inspect}." + "#{policy.class} expected to forbid #{actions}, but allowed " \ + "#{forbidden_actions} for #{policy.user.inspect}." end end end RSpec::Matchers.define :permit_edit_and_update_actions do