Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Last active November 21, 2015 01:48
Show Gist options
  • Select an option

  • Save rsutphin/d4bf91bef5f24b7bc3d9 to your computer and use it in GitHub Desktop.

Select an option

Save rsutphin/d4bf91bef5f24b7bc3d9 to your computer and use it in GitHub Desktop.

Revisions

  1. rsutphin revised this gist Nov 21, 2015. No changes.
  2. rsutphin created this gist Nov 20, 2015.
    3 changes: 3 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    source 'https://rubygems.org'

    gem 'cancancan'
    45 changes: 45 additions & 0 deletions demo.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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