提交 16065b4f 编写于 作者: J Jon Leighton 提交者: Aaron Patterson

Some basic tests for the :foreign_type option on belongs_to, which was...

Some basic tests for the :foreign_type option on belongs_to, which was previously completely untested.
上级 60cf65de
......@@ -579,4 +579,23 @@ def test_polymorphic_counter_cache
end
end
end
def test_polymorphic_with_custom_foreign_type
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
groucho = members(:groucho)
other = members(:some_other_guy)
assert_equal groucho, sponsor.sponsorable
assert_equal groucho, sponsor.thing
sponsor.thing = other
assert_equal other, sponsor.sponsorable
assert_equal other, sponsor.thing
sponsor.sponsorable = groucho
assert_equal groucho, sponsor.sponsorable
assert_equal groucho, sponsor.thing
end
end
......@@ -21,12 +21,13 @@
require 'models/membership'
require 'models/club'
require 'models/categorization'
require 'models/sponsor'
class EagerAssociationTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
:companies, :accounts, :tags, :taggings, :people, :readers, :categorizations,
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
:developers, :projects, :developers_projects, :members, :memberships, :clubs
:developers, :projects, :developers_projects, :members, :memberships, :clubs, :sponsors
def setup
# preheat table existence caches
......@@ -913,4 +914,14 @@ def test_preloading_has_many_through_with_uniq
assert_equal 1, mary.unique_categorized_posts.length
assert_equal 1, mary.unique_categorized_post_ids.length
end
def test_preloading_polymorphic_with_custom_foreign_type
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
groucho = members(:groucho)
sponsor = assert_queries(2) {
Sponsor.includes(:thing).where(:id => sponsor.id).first
}
assert_no_queries { assert_equal groucho, sponsor.thing }
end
end
class Sponsor < ActiveRecord::Base
belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
belongs_to :sponsorable, :polymorphic => true
end
\ No newline at end of file
belongs_to :thing, :polymorphic => true, :foreign_type => :sponsorable_type, :foreign_key => :sponsorable_id
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册