Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit b145a725f84b9f01a63370249f61c61725ab8927 2 parents b1bb9d1 + b2db874
@josevalim josevalim authored
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 )
Please sign in to comment.
Something went wrong with that request. Please try again.