has_one :through supports :source_type. Fix up some tests. References #4756.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9075 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2008-03-22 02:20:37 +00:00
parent f921a96e85
commit 5c1be2812d
5 changed files with 32 additions and 9 deletions

@ -165,8 +165,13 @@ def preload_through_records(records, reflection, through_association)
through_records = []
records.compact.each do |record|
proxy = record.send(through_association)
through_records << proxy.target
proxy.reset
if proxy.respond_to?(:target)
through_records << proxy.target
proxy.reset
else # this is a has_one :through reflection
through_records << proxy if proxy
end
end
through_records = through_records.flatten
else

@ -1326,7 +1326,7 @@ def create_has_one_reflection(association_id, options)
def create_has_one_through_reflection(association_id, options)
options.assert_valid_keys(
:class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source
:class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type
)
create_reflection(:has_one, association_id, options, self)
end

@ -526,16 +526,27 @@ def has_one_through_to_has_many
end
def test_has_one_through_eager_loading
members = Member.find(:all, :include => :club)
assert_equal 2, members.size
members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].club}
end
def test_has_one_through_eager_loading_through_polymorphic
members = Member.find(:all, :include => :sponsor_club)
assert_equal 2, members.size
members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].sponsor_club}
end
def test_has_one_through_polymorphic_with_source_type
assert_equal members(:groucho), clubs(:moustache_club).sponsored_member
end
def test_eager_has_one_through_polymorphic_with_source_type
clubs = Club.find(:all, :include => :sponsored_member, :conditions => ["name = ?","Moustache and Eyebrow Fancier Club"])
# Only the eyebrow fanciers club has a sponsored_member
assert_not_nil assert_no_queries {clubs[0].sponsored_member}
end
end
class HasManyAssociationsTest < ActiveRecord::TestCase

@ -1,3 +1,9 @@
moustache_club_sponsor_for_groucho:
sponsor_club: moustache_club
sponsorable: groucho (Member)
sponsorable: groucho (Member)
boring_club_sponsor_for_groucho:
sponsor_club: boring_club
sponsorable: some_other_guy (Member)
crazy_club_sponsor_for_groucho:
sponsor_club: crazy_club
sponsorable: some_other_guy (Member)

@ -2,5 +2,6 @@ class Club < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships
has_many :current_memberships
has_many :sponsors
has_one :sponsor
has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member"
end