Skip to content

Instantly share code, notes, and snippets.

@jaredbeck
Created July 9, 2017 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredbeck/da27e8d2135ca85b3f5db4067f29d986 to your computer and use it in GitHub Desktop.
Save jaredbeck/da27e8d2135ca85b3f5db4067f29d986 to your computer and use it in GitHub Desktop.
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.1.0' # use correct rails version
gem 'cancancan' # use correct cancancan version
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 :bananas, force: true do |t|
t.string :species
end
end
class Banana < ActiveRecord::Base
end
class Ability
include CanCan::Ability
def initialize(user)
can :eat, Banana, species: /Musa/
end
end
class BugTest < Minitest::Test
def test_bug
ability = Ability.new(nil)
banana = Banana.new(species: 'Musa acuminata')
assert(
ability.can?(:eat, banana),
"The `banana` is of the family /Musa/, therefore I should be able to eat it."
)
# cancan is comparing the regex /Musa/ and the string 'Musa acuminata' using
# the equality operator `==`, which does not do what users think it should do.
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment