Skip to content
This repository
Browse code

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

…s previously completely untested.
  • Loading branch information...
commit 16065b4f19b77111b7fec343969bcf98635e7e27 1 parent 60cf65d
Jon Leighton authored January 01, 2011 tenderlove committed January 03, 2011
19  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -579,4 +579,23 @@ def test_polymorphic_counter_cache
579 579
       end
580 580
     end
581 581
   end
  582
+
  583
+  def test_polymorphic_with_custom_foreign_type
  584
+    sponsor = sponsors(:moustache_club_sponsor_for_groucho)
  585
+    groucho = members(:groucho)
  586
+    other   = members(:some_other_guy)
  587
+
  588
+    assert_equal groucho, sponsor.sponsorable
  589
+    assert_equal groucho, sponsor.thing
  590
+
  591
+    sponsor.thing = other
  592
+
  593
+    assert_equal other, sponsor.sponsorable
  594
+    assert_equal other, sponsor.thing
  595
+
  596
+    sponsor.sponsorable = groucho
  597
+
  598
+    assert_equal groucho, sponsor.sponsorable
  599
+    assert_equal groucho, sponsor.thing
  600
+  end
582 601
 end
13  activerecord/test/cases/associations/eager_test.rb
@@ -21,12 +21,13 @@
21 21
 require 'models/membership'
22 22
 require 'models/club'
23 23
 require 'models/categorization'
  24
+require 'models/sponsor'
24 25
 
25 26
 class EagerAssociationTest < ActiveRecord::TestCase
26 27
   fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
27 28
             :companies, :accounts, :tags, :taggings, :people, :readers, :categorizations,
28 29
             :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
29  
-            :developers, :projects, :developers_projects, :members, :memberships, :clubs
  30
+            :developers, :projects, :developers_projects, :members, :memberships, :clubs, :sponsors
30 31
 
31 32
   def setup
32 33
     # preheat table existence caches
@@ -913,4 +914,14 @@ def test_preloading_has_many_through_with_uniq
913 914
     assert_equal 1, mary.unique_categorized_posts.length
914 915
     assert_equal 1, mary.unique_categorized_post_ids.length
915 916
   end
  917
+
  918
+  def test_preloading_polymorphic_with_custom_foreign_type
  919
+    sponsor = sponsors(:moustache_club_sponsor_for_groucho)
  920
+    groucho = members(:groucho)
  921
+
  922
+    sponsor = assert_queries(2) {
  923
+      Sponsor.includes(:thing).where(:id => sponsor.id).first
  924
+    }
  925
+    assert_no_queries { assert_equal groucho, sponsor.thing }
  926
+  end
916 927
 end
3  activerecord/test/models/sponsor.rb
... ...
@@ -1,4 +1,5 @@
1 1
 class Sponsor < ActiveRecord::Base
2 2
   belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
3 3
   belongs_to :sponsorable, :polymorphic => true
4  
-end
  4
+  belongs_to :thing, :polymorphic => true, :foreign_type => :sponsorable_type, :foreign_key => :sponsorable_id
  5
+end

0 notes on commit 16065b4

Please sign in to comment.
Something went wrong with that request. Please try again.