aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-06-11 05:02:42 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-06-11 05:02:42 +0000
commit813e8ba93bf099dd065512f958a516c6b893a841 (patch)
treeda1752ff980623a9b39ce8748d5ab69e09c5a0fd /activerecord
parentde7a626878ccfab33d6ee30dba5a069fef06e206 (diff)
downloadrails-813e8ba93bf099dd065512f958a516c6b893a841.tar.gz
rails-813e8ba93bf099dd065512f958a516c6b893a841.tar.bz2
rails-813e8ba93bf099dd065512f958a516c6b893a841.zip
Fix Base#inspect when not every attribute is present. Closes #8623.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6995 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb7
-rwxr-xr-xactiverecord/test/base_test.rb5
3 files changed, 10 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e56b12f274..b384fa57da 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -50,7 +50,7 @@
* Document warning that associations names shouldn't be reserved words. #4378 [murphy@cYcnus.de, Josh Susser]
-* Sanitize Base#inspect. #8392 [Nik Wakelin]
+* Sanitize Base#inspect. #8392, #8623 [Nik Wakelin, jnoon]
* Replace the transaction {|transaction|..} semantics with a new Exception ActiveRecord::Rollback. [Koz]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index e1f3dce7ce..8c7b83f82a 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1852,7 +1852,6 @@ module ActiveRecord #:nodoc:
# Format attributes nicely for inspect.
def attribute_for_inspect(attr_name)
- raise "Attribute not present #{attr_name}" unless has_attribute?(attr_name) || new_record?
value = read_attribute(attr_name)
if value.is_a?(String) && value.length > 50
@@ -1947,8 +1946,10 @@ module ActiveRecord #:nodoc:
# Nice pretty inspect.
def inspect
attributes_as_nice_string = self.class.column_names.collect { |name|
- "#{name}: #{attribute_for_inspect(name)}"
- }.join(", ")
+ if has_attribute?(name) || new_record?
+ "#{name}: #{attribute_for_inspect(name)}"
+ end
+ }.compact.join(", ")
"#<#{self.class} #{attributes_as_nice_string}>"
end
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index a64f933f37..a0a6100abd 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -1695,6 +1695,11 @@ class BasicsTest < Test::Unit::TestCase
assert_match /Topic id: nil/, Topic.new.inspect
end
+ def test_inspect_limited_select_instance
+ assert_equal %(#<Topic id: 1>), Topic.find(:first, :select => 'id', :conditions => 'id = 1').inspect
+ assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.find(:first, :select => 'id, title', :conditions => 'id = 1').inspect
+ end
+
def test_attribute_for_inspect
t = topics(:first)
t.content = %(This is some really long content, longer than 50 characters, so I can test that text is truncated correctly by the new ActiveRecord::Base#inspect method! Yay! BOOM!)