Skip to content

Commit

Permalink
Some basic tests for the :foreign_type option on belongs_to, which wa…
Browse files Browse the repository at this point in the history
…s previously completely untested.
  • Loading branch information
jonleighton authored and tenderlove committed Jan 4, 2011
1 parent 60cf65d commit 16065b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -579,4 +579,23 @@ def test_polymorphic_counter_cache
end end
end 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 end
13 changes: 12 additions & 1 deletion activerecord/test/cases/associations/eager_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
require 'models/membership' require 'models/membership'
require 'models/club' require 'models/club'
require 'models/categorization' require 'models/categorization'
require 'models/sponsor'


class EagerAssociationTest < ActiveRecord::TestCase class EagerAssociationTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts, fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
:companies, :accounts, :tags, :taggings, :people, :readers, :categorizations, :companies, :accounts, :tags, :taggings, :people, :readers, :categorizations,
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books, :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 def setup
# preheat table existence caches # preheat table existence caches
Expand Down Expand Up @@ -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_posts.length
assert_equal 1, mary.unique_categorized_post_ids.length assert_equal 1, mary.unique_categorized_post_ids.length
end 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 end
3 changes: 2 additions & 1 deletion activerecord/test/models/sponsor.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
class Sponsor < ActiveRecord::Base class Sponsor < ActiveRecord::Base
belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id" belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id"
belongs_to :sponsorable, :polymorphic => true 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.