Skip to content

Instantly share code, notes, and snippets.

@rsutphin

rsutphin/Gemfile

Last active Nov 21, 2015
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Nov 20, 2015

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

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Nov 21, 2015

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
You can’t perform that action at this time.