Permalink
Browse files

Merge pull request #1297 from cesario/1294-allocated-object-cant-be-i…

…nspected


#1294 : allocated object cant be inspected [closes #1294]
  • Loading branch information...
2 parents b1bb9d1 + b2db874 commit b145a725f84b9f01a63370249f61c61725ab8927 @josevalim josevalim committed May 25, 2011
Showing with 18 additions and 6 deletions.
  1. +10 −6 activerecord/lib/active_record/base.rb
  2. +8 −0 activerecord/test/cases/attribute_methods_test.rb
View
16 activerecord/lib/active_record/base.rb
@@ -1870,12 +1870,16 @@ def readonly!
# 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
View
8 activerecord/test/cases/attribute_methods_test.rb
@@ -109,6 +109,14 @@ def test_respond_to_with_allocated_object
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, "#<Topic not initialized>"
+ end
+
def test_array_content
topic = Topic.new
topic.content = %w( one two three )

0 comments on commit b145a72

Please sign in to comment.