From bf159aa5fc9ed4d68c4975dab37dfaf58d29a08c Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Wed, 25 May 2011 13:52:11 +0200 Subject: Test AR::Base#inspect on an allocated object not yet initialized --- activerecord/test/cases/attribute_methods_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 5074ae50ab..54c4d4ae90 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -109,6 +109,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_respond_to topic, :title end + # IRB inspects the return value of "MyModel.allocate" + # by inspecting it. + def test_allocated_object_can_be_inspected + topic = Topic.allocate + assert_nothing_raised { topic.inspect } + assert topic.inspect, "#" + end + def test_array_content topic = Topic.new topic.content = %w( one two three ) -- cgit v1.2.3 From b2db8740eabbb9b9c6afb237530d33ef7fc807e9 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Wed, 25 May 2011 13:52:25 +0200 Subject: Fix the AR::Base#inspect method [closes #1294] --- activerecord/lib/active_record/base.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 99930e7697..cb2c621c79 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1870,12 +1870,16 @@ MSG # Returns the contents of the record as a nicely formatted string. def inspect - attributes_as_nice_string = self.class.column_names.collect { |name| - if has_attribute?(name) - "#{name}: #{attribute_for_inspect(name)}" - end - }.compact.join(", ") - "#<#{self.class} #{attributes_as_nice_string}>" + inspection = if @attributes + self.class.column_names.collect { |name| + if has_attribute?(name) + "#{name}: #{attribute_for_inspect(name)}" + end + }.compact.join(", ") + else + "not initialized" + end + "#<#{self.class} #{inspection}>" end protected -- cgit v1.2.3