Sha256: 04fb42e598246a862a17625661ebb5e4dfb20f995623a894c9caabfce82cf640

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

# rubocop:disable Metrics/BlockLength
rspec_module = defined?(RSpec::Core) ? 'RSpec' : 'Spec' # RSpec 1 compatability

if rspec_module == 'RSpec'
  require 'rspec/core'
  require 'rspec/expectations'
else
  ActiveSupport::Deprecation
    .warn('RSpec < 3 will not be supported in the CanCanCan >= 2.0.0')
end

# Allow passing args like
#
#     be_able_to :read, thing
#
# or
#
#     be_able_to [:read, :update], thing
#
Kernel.const_get(rspec_module)::Matchers.define :be_able_to do |*args|
  match do |ability|
    actions = Array.wrap(args[0])
    actions.all? { |action| ability.can?(action, *args[1..-1]) }
  end

  # Check that RSpec is < 2.99
  if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
    alias_method :failure_message, :failure_message_for_should
    alias_method :failure_message_when_negated, :failure_message_for_should_not
  end

  failure_message do
    resource = args[1]
    if resource.instance_of?(Class)
      "expected to be able to #{args.map(&:to_s).join(' ')}"
    else
      "expected to be able to #{args.map(&:inspect).join(' ')}"
    end
  end

  failure_message_when_negated do
    resource = args[1]
    if resource.instance_of?(Class)
      "expected not to be able to #{args.map(&:to_s).join(' ')}"
    else
      "expected not to be able to #{args.map(&:inspect).join(' ')}"
    end
  end

  description do
    action = args[0].to_s
    resource = args[1].class.name
    "be able to #{action} #{resource}"
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
voyage-1.44.0.14 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.13 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.11 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.12 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.10 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.9 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.8 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.7 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.6 lib/voyage/templates/custom_cancan_matchers.rb
voyage-1.44.0.5 lib/voyage/templates/custom_cancan_matchers.rb