lib/pundit/matchers.rb in pundit-matchers-1.0.2 vs lib/pundit/matchers.rb in pundit-matchers-1.1.0

- old
+ new

@@ -6,128 +6,168 @@ match do |policy| policy.public_send("#{action}?") end failure_message do |policy| - "#{policy.class} does not permit #{action} on #{policy.record} for " + + "#{policy.class} does not permit #{action} on #{policy.record} for " \ "#{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not forbid #{action} on #{policy.record} for " + + "#{policy.class} does not forbid #{action} on #{policy.record} for " \ "#{policy.user.inspect}." end end RSpec::Matchers.define :permit_new_and_create_actions do match do |policy| - policy.new? and policy.create? + policy.new? && policy.create? end failure_message do |policy| - "#{policy.class} does not permit the new or create action on " + + "#{policy.class} does not permit the new or create action on " \ "#{policy.record} for #{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not forbid the new or create action on " + + "#{policy.class} does not forbid the new or create action on " \ "#{policy.record} for #{policy.user.inspect}." end end RSpec::Matchers.define :permit_edit_and_update_actions do match do |policy| - policy.edit? and policy.update? + policy.edit? && policy.update? end failure_message do |policy| - "#{policy.class} does not permit the edit or update action on " + + "#{policy.class} does not permit the edit or update action on " \ "#{policy.record} for #{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not forbid the edit or update action on " + + "#{policy.class} does not forbid the edit or update action on " \ "#{policy.record} for #{policy.user.inspect}." end end RSpec::Matchers.define :permit_mass_assignment_of do |attribute| match do |policy| - policy.permitted_attributes.include? attribute + if @action + policy.send("permitted_attributes_for_#{@action}").include? attribute + else + policy.permitted_attributes.include? attribute + end end + chain :for_action do |action| + @action = action + end + failure_message do |policy| - "#{policy.class} does not permit the mass assignment of the " + - "#{attribute} attribute for #{policy.user.inspect}." + if @action + "#{policy.class} does not permit the mass assignment of the " \ + "#{attribute} attribute, when authorising the #{@action} action, " \ + "for #{policy.user.inspect}." + else + "#{policy.class} does not permit the mass assignment of the " \ + "#{attribute} attribute for #{policy.user.inspect}." + end end failure_message_when_negated do |policy| - "#{policy.class} does not forbid the mass assignment of the " + - "#{attribute} attribute for #{policy.user.inspect}." + if @action + "#{policy.class} does not forbid the mass assignment of the " \ + "#{attribute} attribute, when authorising the #{@action} action, " \ + "for #{policy.user.inspect}." + else + "#{policy.class} does not forbid the mass assignment of the " \ + "#{attribute} attribute for #{policy.user.inspect}." + end end end RSpec::Matchers.define :forbid_action do |action| match do |policy| !policy.public_send("#{action}?") end failure_message do |policy| - "#{policy.class} does not forbid #{action} on #{policy.record} for " + + "#{policy.class} does not forbid #{action} on #{policy.record} for " \ "#{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not permit #{action} on #{policy.record} for " + + "#{policy.class} does not permit #{action} on #{policy.record} for " \ "#{policy.user.inspect}." end end RSpec::Matchers.define :forbid_new_and_create_actions do match do |policy| - !policy.new? and !policy.create? + !policy.new? && !policy.create? end failure_message do |policy| - "#{policy.class} does not forbid the new or create action on " + + "#{policy.class} does not forbid the new or create action on " \ "#{policy.record} for #{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not permit the new or create action on " + + "#{policy.class} does not permit the new or create action on " \ "#{policy.record} for #{policy.user.inspect}." end end RSpec::Matchers.define :forbid_edit_and_update_actions do match do |policy| - !policy.edit? and !policy.update? + !policy.edit? && !policy.update? end failure_message do |policy| - "#{policy.class} does not forbid the edit or update action on " + + "#{policy.class} does not forbid the edit or update action on " \ "#{policy.record} for #{policy.user.inspect}." end failure_message_when_negated do |policy| - "#{policy.class} does not permit the edit or update action on " + + "#{policy.class} does not permit the edit or update action on " \ "#{policy.record} for #{policy.user.inspect}." end end RSpec::Matchers.define :forbid_mass_assignment_of do |attribute| match do |policy| - policy.permitted_attributes.exclude? attribute + if @action + policy.send("permitted_attributes_for_#{@action}").exclude? attribute + else + policy.permitted_attributes.exclude? attribute + end end + chain :for_action do |action| + @action = action + end + failure_message do |policy| - "#{policy.class} does not forbid the mass assignment of the " + - "#{attribute} attribute for #{policy.user.inspect}." + if @action + "#{policy.class} does not forbid the mass assignment of the " \ + "#{attribute} attribute, when authorising the #{@action} action, " \ + "for #{policy.user.inspect}." + else + "#{policy.class} does not forbid the mass assignment of the " \ + "#{attribute} attribute for #{policy.user.inspect}." + end end failure_message_when_negated do |policy| - "#{policy.class} does not permit the mass assignment of the " + - "#{attribute} attribute for #{policy.user.inspect}." + if @action + "#{policy.class} does not permit the mass assignment of the " \ + "#{attribute} attribute, when authorising the #{@action} action, " \ + "for #{policy.user.inspect}." + else + "#{policy.class} does not permit the mass assignment of the " \ + "#{attribute} attribute for #{policy.user.inspect}." + end end end end end