diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-10 02:00:14 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-11-10 02:00:14 +0000 |
commit | 63df6eb3821ba9723a3478f6331bd14b81be8140 (patch) | |
tree | e3245c8d2ded2289570cf3e8105f8cd6b5a6ee6f /activerecord | |
parent | d97a84fb593746caa5806704fe3018aa3f3fc422 (diff) | |
download | rails-63df6eb3821ba9723a3478f6331bd14b81be8140.tar.gz rails-63df6eb3821ba9723a3478f6331bd14b81be8140.tar.bz2 rails-63df6eb3821ba9723a3478f6331bd14b81be8140.zip |
Don't inspect unloaded associations. Closes #2905.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5478 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 4 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 29 |
3 files changed, 35 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index bf344d2668..5564a750af 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Don't inspect unloaded associations. #2905 [lmarlow] + * SQLite: use AUTOINCREMENT primary key in >= 3.1.0. #6588 [careo] * Cache inheritance_column. #6592 [Stefan Kaes] diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 1b929f28bd..f84a2a89b7 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -72,6 +72,10 @@ module ActiveRecord loaded end + def inspect + loaded? ? @target.inspect : "<#{@reflection.name} not loaded yet>" + end + protected def dependent? @reflection.options[:dependent] || false diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index f1b02cfda5..f962537966 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -87,6 +87,35 @@ class AssociationProxyTest < Test::Unit::TestCase david.posts_with_extension.first # force load target assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target end + + def test_inspect_does_not_load_target + david = authors(:david) + not_loaded_string = '<posts not loaded yet>' + not_loaded_re = Regexp.new(not_loaded_string) + + 2.times do + assert !david.posts.loaded?, "Posts should not be loaded yet" + assert_match not_loaded_re, david.inspect + assert_equal not_loaded_string, david.posts.inspect + + assert !david.posts.empty?, "There should be more than one post" + assert !david.posts.loaded?, "Posts should still not be loaded yet" + assert_match not_loaded_re, david.inspect + assert_equal not_loaded_string, david.posts.inspect + + assert !david.posts.find(:all).empty?, "There should be more than one post" + assert !david.posts.loaded?, "Posts should still not be loaded yet" + assert_match not_loaded_re, david.inspect + assert_equal not_loaded_string, david.posts.inspect + + assert !david.posts(true).empty?, "There should be more than one post" + assert david.posts.loaded?, "Posts should be loaded now" + assert_no_match not_loaded_re, david.inspect + assert_not_equal not_loaded_string, david.posts.inspect + + david.reload + end + end end class HasOneAssociationsTest < Test::Unit::TestCase |