aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-03-21 18:09:03 +0000
committerRick Olson <technoweenie@gmail.com>2008-03-21 18:09:03 +0000
commit273b21faa911681ed4b6c748676146e0f6eed0a0 (patch)
tree7454ae8ead82fbe980c9f3630289c672f247b232 /activerecord/test/cases
parentd8f76e66a19435239eec6f65f910133c6059cd60 (diff)
downloadrails-273b21faa911681ed4b6c748676146e0f6eed0a0.tar.gz
rails-273b21faa911681ed4b6c748676146e0f6eed0a0.tar.bz2
rails-273b21faa911681ed4b6c748676146e0f6eed0a0.zip
Add has_one :through support, finally. Closes #4756 [thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9067 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb2
-rwxr-xr-xactiverecord/test/cases/associations_test.rb63
2 files changed, 63 insertions, 2 deletions
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index 1034cb67e9..3d59b97f70 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -631,7 +631,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
assert_equal comments.first.post, comments[1].post
end
end
-
+
private
# create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency)
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 50643066b9..768d2b2600 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -20,6 +20,10 @@ require 'models/parrot'
require 'models/pirate'
require 'models/treasure'
require 'models/price_estimate'
+require 'models/club'
+require 'models/member'
+require 'models/membership'
+require 'models/sponsor'
class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
@@ -186,7 +190,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal companies(:first_firm).account, Account.find(1)
assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
end
-
+
def test_has_one_cache_nils
firm = companies(:another_firm)
assert_queries(1) { assert_nil firm.account }
@@ -476,6 +480,63 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
end
+class HasOneThroughAssociationsTest < ActiveRecord::TestCase
+ fixtures :members, :clubs, :memberships, :sponsors
+
+ def setup
+ @member = members(:groucho)
+ end
+
+ def test_has_one_through_with_has_one
+ assert_equal clubs(:boring_club), @member.club
+ end
+
+ def test_has_one_through_with_has_many
+ assert_equal clubs(:moustache_club), @member.favourite_club
+ end
+
+ def test_creating_association_creates_through_record
+ new_member = Member.create(:name => "Chris")
+ new_member.club = Club.create(:name => "LRUG")
+ assert_not_nil new_member.current_membership
+ assert_not_nil new_member.club
+ end
+
+ def test_replace_target_record
+ new_club = Club.create(:name => "Marx Bros")
+ @member.club = new_club
+ @member.reload
+ assert_equal new_club, @member.club
+ end
+
+ def test_replacing_target_record_deletes_old_association
+ assert_no_difference "Membership.count" do
+ new_club = Club.create(:name => "Bananarama")
+ @member.club = new_club
+ @member.reload
+ end
+ end
+
+ def test_has_one_through_polymorphic
+ assert_equal clubs(:moustache_club), @member.sponsor_club
+ end
+
+ def has_one_through_to_has_many
+ assert_equal 2, @member.fellow_members.size
+ end
+
+ def test_has_one_through_eager_loading
+ members = Member.find(:all, :include => :club)
+ assert_equal 2, 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
+ assert_not_nil assert_no_queries {members[0].sponsor_club}
+ end
+end
class HasManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects,