Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Last active November 21, 2015 01:48
Show Gist options
  • Save rsutphin/d4bf91bef5f24b7bc3d9 to your computer and use it in GitHub Desktop.
Save rsutphin/d4bf91bef5f24b7bc3d9 to your computer and use it in GitHub Desktop.
Demonstrating unexpected behavior in cancancan
require 'cancan'
Hat = Struct.new(:color)
class MyAbility
include CanCan::Ability
def initialize
can :see, Hat, color: "red"
end
end
# ---
ability = MyAbility.new
if ability.can?(:see, Hat.new("red"))
$stderr.puts "✅ You can see a particular red hat, as expected"
else
$stderr.puts "❌ You can't see a particular red hat. Weird."
end
if ability.can?(:see, Hat.new("blue"))
$stderr.puts "❌ You can see a particular blue hat. That shouldn't happen."
else
$stderr.puts "✅ You can't see a particular blue hat. Good."
end
if ability.can?(:see, Hat, color: "red")
$stderr.puts "✅ You can see any red hat, as expected"
else
$stderr.puts "❌ You can't see any red hat. Weird."
end
if ability.can?(:see, Hat, color: "blue")
$stderr.puts "❌ You can see any blue hat. That shouldn't happen."
else
$stderr.puts "✅ You can't see any blue hat. Good."
end
if ability.can?(:see, Hat)
$stderr.puts "❌ You can see any hat. That shouldn't happen."
else
$stderr.puts "✅ You can't see just any hat. Good."
end
source 'https://rubygems.org'
gem 'cancancan'
@rsutphin
Copy link
Author

The output I get with all versions of cancancan I've tested is

✅ You can see a particular red hat, as expected
✅ You can't see a particular blue hat. Good.
✅ You can see any red hat, as expected
❌ You can see any blue hat. That shouldn't happen.
❌ You can see any hat. That shouldn't happen.

@rsutphin
Copy link
Author

Issue is here: cancancan/issues#277. Summary: working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment