Permalink
Browse files

Some basic tests for the :foreign_type option on belongs_to, which wa…

…s previously completely untested.
  • Loading branch information...
1 parent 60cf65d commit 16065b4f19b77111b7fec343969bcf98635e7e27 @jonleighton jonleighton committed with tenderlove Jan 1, 2011
View
19 activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -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
View
13 activerecord/test/cases/associations/eager_test.rb
@@ -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
View
3 activerecord/test/models/sponsor.rb
@@ -1,4 +1,5 @@
class Sponsor < ActiveRecord::Base
belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
belongs_to :sponsorable, :polymorphic => true
-end
+ belongs_to :thing, :polymorphic => true, :foreign_type => :sponsorable_type, :foreign_key => :sponsorable_id
+end

0 comments on commit 16065b4

Please sign in to comment.