Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Last active August 29, 2015 14:02
Show Gist options
  • Save lucasmazza/923955809f41223a4b02 to your computer and use it in GitHub Desktop.
Save lucasmazza/923955809f41223a4b02 to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle --quiet'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts
create_table :slugs do |t|
t.string :name
t.integer :sluggable_id
t.string :sluggable_type
end
end
class Slug < ActiveRecord::Base
end
class Post < ActiveRecord::Base
has_one :slug, as: :sluggable
scope :by_slug, ->(slug) { joins(:slug).where(slugs: { name: slug }) }
end
class BugTest < Minitest::Test
def setup
Post.delete_all
Slug.delete_all
@post = Post.create!(slug: Slug.new(name: 'the-slug'))
@relation = Post.by_slug('the-slug')
end
def test_polymorphic_count
assert_equal 1, @relation.count
end
def test_polymorphic_load
assert_equal [@post], @relation.to_a
end
def test_polymorphic_exists
assert_equal true, @relation.exists?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment