Sha256: fa5bbf279d1ceadc07f6760a44cd2cbc8a1bb4eb09f181a3e4450d169aa9c39e

Contents?: true

Size: 1.03 KB

Versions: 9

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module ActionPolicy
  module RSpec
    # Adds Pundit-style syntax for testing policies
    module PunditSyntax # :nodoc: all
      module Matchers
        extend ::RSpec::Matchers::DSL

        matcher :permit do |user, record|
          match do |policy|
            permissions.all? do |permission|
              policy.new(record, user: user).apply(permission)
            end
          end
        end
      end

      module DSL
        def permissions(*list, &block)
          describe list.to_sentence do
            let(:permissions) { list }

            instance_eval(&block)
          end
        end
      end

      module PolicyExampleGroup
        include Matchers

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

RSpec.configure do |config|
  config.include(
    ActionPolicy::RSpec::PunditSyntax::PolicyExampleGroup,
    type: :policy,
    example_group: { file_path: %r{spec/policies} }
  )
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
action_policy-0.2.4 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.2.3 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.2.2 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.2.0 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.1.4 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.1.3 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.1.2 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.1.1 lib/action_policy/rspec/pundit_syntax.rb
action_policy-0.1.0 lib/action_policy/rspec/pundit_syntax.rb