From 7cba6a37848ba96b4decec885779fb309d71c339 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Mon, 14 Nov 2011 22:57:15 -0800 Subject: association methods are now generated in modules Instead of generating association methods directly in the model class, they are generated in an anonymous module which is then included in the model class. There is one such module for each association. The only subtlety is that the generated_attributes_methods module (from ActiveModel) must be forced to be included before association methods are created so that attribute methods will not shadow association methods. --- activerecord/test/cases/associations_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/test/cases/associations_test.rb') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index ffe2993e0f..a9094b7a8b 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require 'models/computer' require 'models/developer' require 'models/project' require 'models/company' @@ -273,3 +274,14 @@ class OverridingAssociationsTest < ActiveRecord::TestCase ) end end + +class GeneratedMethodsTest < ActiveRecord::TestCase + fixtures :developers, :computers + def test_association_methods_override_attribute_methods_of_same_name + assert_equal(developers(:david), computers(:workstation).developer) + # this next line will fail if the attribute methods module is generated lazily + # after the association methods module is generated + assert_equal(developers(:david), computers(:workstation).developer) + assert_equal(developers(:david).id, computers(:workstation)[:developer]) + end +end \ No newline at end of file -- cgit v1.2.3 From 9cdf33af0bc46fde1ad50346b8271251c2b4aa69 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Tue, 15 Nov 2011 23:30:25 -0800 Subject: add test for super-ing to association methods --- activerecord/test/cases/associations_test.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations_test.rb') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index a9094b7a8b..0f75029215 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -276,7 +276,7 @@ class OverridingAssociationsTest < ActiveRecord::TestCase end class GeneratedMethodsTest < ActiveRecord::TestCase - fixtures :developers, :computers + fixtures :developers, :computers, :posts, :comments def test_association_methods_override_attribute_methods_of_same_name assert_equal(developers(:david), computers(:workstation).developer) # this next line will fail if the attribute methods module is generated lazily @@ -284,4 +284,14 @@ class GeneratedMethodsTest < ActiveRecord::TestCase assert_equal(developers(:david), computers(:workstation).developer) assert_equal(developers(:david).id, computers(:workstation)[:developer]) end -end \ No newline at end of file + + def test_model_method_overrides_association_method + Post.class_eval <<-"RUBY" + has_one :first_comment, :class_name => 'Comment', :order => 'id ASC' + def first_comment + super.body + end + RUBY + assert_equal(comments(:greetings).body, posts(:welcome).first_comment) + end +end -- cgit v1.2.3 From c347b3c06c2867badce5e22ecfbed3e972960c29 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Tue, 29 Nov 2011 09:14:21 -0800 Subject: don't change class definition in test case --- activerecord/test/cases/associations_test.rb | 6 ------ 1 file changed, 6 deletions(-) (limited to 'activerecord/test/cases/associations_test.rb') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 0f75029215..efe71d1771 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -286,12 +286,6 @@ class GeneratedMethodsTest < ActiveRecord::TestCase end def test_model_method_overrides_association_method - Post.class_eval <<-"RUBY" - has_one :first_comment, :class_name => 'Comment', :order => 'id ASC' - def first_comment - super.body - end - RUBY assert_equal(comments(:greetings).body, posts(:welcome).first_comment) end end -- cgit v1.2.3 From c99d507fccca2e9e4d12e49b4387e007c5481ae9 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 29 Dec 2011 14:26:38 +0000 Subject: Deprecate implicit eager loading. Closes #950. --- activerecord/test/cases/associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations_test.rb') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index efe71d1771..df657c1eb4 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -29,7 +29,7 @@ class AssociationsTest < ActiveRecord::TestCase molecule.electrons.create(:name => 'electron_1') molecule.electrons.create(:name => 'electron_2') - liquids = Liquid.includes(:molecules => :electrons).where('molecules.id is not null') + liquids = Liquid.eager_load(:molecules => :electrons).where('molecules.id is not null') assert_equal 1, liquids[0].molecules.length end -- cgit v1.2.3