Skip to content

Instantly share code, notes, and snippets.

@oboxodo oboxodo/main.rb forked from coorasse/main.rb
Last active Feb 22, 2018

Embed
What would you like to do?
CanCanCan Issue #485
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '5.0.6'
gem 'cancancan', '2.1.3' # Fails!
# gem 'cancancan', '1.17.0' # Passes!
gem 'sqlite3' # use another DB if necessary
end
require 'active_record'
require 'cancancan'
require 'cancan/model_adapters/active_record_adapter'
require 'cancan/model_adapters/active_record_4_adapter'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# create your tables here
ActiveRecord::Schema.define do
create_table :books, force: true do |t|
t.integer :user_id
end
create_table :users, force: true do |t|
t.string :name
end
end
class Book < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :books
end
class Ability
include CanCan::Ability
def initialize(user)
can :manage, :all
# This is OK according to [Ability Precedence](https://github.com/CanCanCommunity/cancancan/wiki/Ability-Precedence)
cannot :do_something, Book
cannot :analyze, nil
end
end
class BugTest < Minitest::Test
def setup
@user = User.create!
@book = Book.create(user: @user)
@ability = Ability.new(@user)
end
def test_user_cannot_do_something_with_book
assert_equal false, @ability.can?(:do_something, @book)
end
def test_user_can_do_something_with_nil
assert_equal true, @ability.can?(:do_something, nil)
end
def test_user_can_analyze_a_book
assert_equal true, @ability.can?(:analyze, @book)
end
# This one passes with 1.17.0 but fails on 2.1.3
def test_user_cannot_analyze_a_nil
assert_equal false, @ability.can?(:analyze, nil)
end
end
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.