SHARE
TWEET

Untitled

a guest Apr 25th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. begin
  2.   require 'bundler/inline'
  3. rescue LoadError => e
  4.   $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  5.   raise e
  6. end
  7.  
  8. gemfile(true) do
  9.   source 'https://rubygems.org'
  10.   gem 'rails', '5.1.7'  # use correct rails version
  11.   gem 'cancancan', '3.0.1' # use correct cancancan version
  12.   gem 'sqlite3', '1.3.9' # use another DB if necessary
  13. end
  14.  
  15. require 'active_record'
  16. require 'cancancan'
  17. require 'cancan/model_adapters/active_record_adapter'
  18. require 'cancan/model_adapters/active_record_4_adapter'
  19. require 'cancan/model_adapters/active_record_5_adapter'
  20. require 'cancan/model_adapters/conditions_normalizer'
  21. require 'minitest/autorun'
  22. require 'logger'
  23.  
  24. # This connection will do for database-independent bug reports.
  25. ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
  26. ActiveRecord::Base.logger = Logger.new(STDOUT)
  27.  
  28. # create your tables here
  29. ActiveRecord::Schema.define do
  30.   create_table :foos, force: true  do |t|
  31.   end
  32.  
  33.   create_table :connections, force: true  do |t|
  34.     t.references :foo
  35.     t.references :holder, polymorphic: true
  36.   end
  37.  
  38.   create_table :bars, force: true  do |t|
  39.   end
  40. end
  41.  
  42. class A < ActiveRecord::Base
  43. end
  44.  
  45. class B < ActiveRecord::Base
  46. end
  47.  
  48. class Connection < ActiveRecord::Base
  49.   has_one :self_ref, class_name: 'Connection', foreign_key: :id
  50.   has_one :a, through: :self_ref, source: :owner, source_type: A
  51.   has_one :b, through: :self_ref, source: :owner, source_type: B
  52.  
  53.   belongs_to :owner, polymorphic: true
  54.   belongs_to :bar
  55. end
  56.  
  57. class Bar < ActiveRecord::Base
  58.   has_many :connections, as: :owner
  59. end
  60.  
  61. class Ability
  62.   include CanCan::Ability
  63.  
  64.   def initialize(user)
  65.     can :read, Bar, connections: { a: { id: 1 } }
  66.     can :read, Bar, connections: { b: { id: 1 } }
  67.   end
  68. end
  69.  
  70. class BugTest < Minitest::Test
  71.   def test_bug
  72.     ability = Ability.new(nil)
  73.     Bar.accessible_by(ability)
  74.   end
  75. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top