Skip to content

Instantly share code, notes, and snippets.

@shekibobo
Last active August 29, 2015 13:57
Show Gist options
  • Save shekibobo/9673557 to your computer and use it in GitHub Desktop.
Save shekibobo/9673557 to your computer and use it in GitHub Desktop.
double_polymorphic_any_of_test.rb
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
gem 'activerecord_any_of'
gem 'pry'
gem 'pg'
GEMFILE
system 'bundle'
end
PG_SPEC = {
:adapter => 'postgresql',
:host => 'localhost',
:database => 'activerecord_any_of_test',
:username => 'postgres',
:encoding => 'utf8'
}
require 'bundler'
Bundler.setup(:default)
require 'rails'
require 'pg'
require 'active_record'
require 'activerecord_any_of'
require 'minitest/autorun'
require 'logger'
require 'pry'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(PG_SPEC.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database PG_SPEC[:database] rescue nil
# create new
ActiveRecord::Base.connection.create_database(PG_SPEC[:database])
ActiveRecord::Base.establish_connection(PG_SPEC)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :organizations do |t|
end
create_table :universities do |t|
end
create_table :memberships do |t|
t.references :group, polymorphic: true
t.references :member, polymorphic: true
end
create_table :users do |t|
end
create_table :admin_users do |t|
end
end
module Group
extend ActiveSupport::Concern
included do
has_many :memberships, as: :group
has_many :users, through: :memberships, source: :member, source_type: "User"
has_many :admin_users, through: :memberships, source: :member, source_type: "AdminUser"
end
end
module Member
extend ActiveSupport::Concern
included do
has_many :memberships, as: :member
has_many :organizations, through: :memberships, source: :group, source_type: "Organization"
has_many :universities, through: :memberships, source: :group, source_type: "University"
end
end
class User < ActiveRecord::Base
include Member
end
class AdminUser < ActiveRecord::Base
include Member
end
class Membership < ActiveRecord::Base
belongs_to :member, polymorphic: true
belongs_to :group, polymorphic: true
end
class Organization < ActiveRecord::Base
include Group
end
class University < ActiveRecord::Base
include Group
end
class BugTest < Minitest::Test
def test_any_of_polymorphic_scopes
org_user = User.create!
uni_user = User.create!
org_admin_user = AdminUser.create!
uni_admin_user = AdminUser.create!
organization = Organization.create!
university = University.create!
organization.users << org_user
university.users << uni_user
organization.admin_users << org_admin_user
university.admin_users << uni_admin_user
assert_includes [org_user, uni_user], *User.where.any_of(organization.users, university.users).to_a
assert_includes [org_admin_user, uni_admin_user], *AdminUser.where.any_of(organization.admin_users, university.admin_users).to_a
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment