Sha256: 5714ce6dd5b230e6ee822fbda7767e07ae1c5adf2b2e59b51e12ff11cf4973a0

Contents?: true

Size: 1.48 KB

Versions: 27

Compression:

Stored size: 1.48 KB

Contents

module Pundit
  module RSpec
    module Matchers
      extend ::RSpec::Matchers::DSL

      matcher :permit do |user, site, resource|
        match_for_should do |policy|
          permissions.all? { |permission| puts permission.inspect; policy.new(user, site, resource).public_send(permission) }
        end

        match_for_should_not do |policy|
          permissions.none? { |permission| policy.new(user, site, resource).public_send(permission) }
        end

        failure_message_for_should do |policy|
          "Expected #{policy} to grant #{permissions.to_sentence} on #{resource}/#{site} but it didn't"
        end

        failure_message_for_should_not do |policy|
          "Expected #{policy} not to grant #{permissions.to_sentence} on #{resource}/#{site} but it did"
        end

        def permissions
          current_example = ::RSpec.respond_to?(:current_example) ? ::RSpec.current_example : example
          current_example.metadata[:permissions]
        end
      end
    end

    module DSL
      def permissions(*list, &block)
        describe(list.to_sentence, permissions: list, caller: caller) { instance_eval(&block) }
      end
    end

    module PolicyExampleGroup
      include Pundit::RSpec::Matchers

      def self.included(base)
        base.metadata[:type] = :policy
        base.extend Pundit::RSpec::DSL
        super
      end
    end
  end
end

RSpec.configure do |config|
  config.include Pundit::RSpec::PolicyExampleGroup, type: :policy, file_path: /spec\/policies/
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
locomotivecms-3.3.0 spec/support/pundit_matcher.rb
locomotivecms-3.3.0.rc3 spec/support/pundit_matcher.rb
locomotivecms-3.3.0.rc2 spec/support/pundit_matcher.rb
locomotivecms-3.1.2 spec/support/pundit_matcher.rb
locomotivecms-3.2.1 spec/support/pundit_matcher.rb
locomotivecms-3.3.0.rc1 spec/support/pundit_matcher.rb
locomotivecms-3.2.0 spec/support/pundit_matcher.rb
locomotivecms-3.2.0.rc2 spec/support/pundit_matcher.rb
locomotivecms-3.2.0.rc1 spec/support/pundit_matcher.rb
locomotivecms-3.1.1 spec/support/pundit_matcher.rb
locomotivecms-3.1.0 spec/support/pundit_matcher.rb
locomotivecms-3.1.0.rc3 spec/support/pundit_matcher.rb
locomotivecms-3.1.0.rc2 spec/support/pundit_matcher.rb
locomotivecms-3.1.0.rc1 spec/support/pundit_matcher.rb
locomotivecms-3.0.1 spec/support/pundit_matcher.rb
locomotivecms-3.0.0 spec/support/pundit_matcher.rb
locomotivecms-3.0.0.rc7 spec/support/pundit_matcher.rb
locomotivecms-3.0.0.rc6 spec/support/pundit_matcher.rb
locomotivecms-3.0.0.rc5 spec/support/pundit_matcher.rb
locomotivecms-3.0.0.rc4 spec/support/pundit_matcher.rb