Skip to content

Commit 3055f59

Browse files
authored
Merge pull request rails#25559 from kmcphillips/master
Do not inspect array of over 10 elements
2 parents 160cc33 + a023fd6 commit 3055f59

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Inspecting an object with an associated array of over 10 elements no longer
2+
truncates the array, preventing `inspect` from looping infinitely in some
3+
cases.
4+
5+
*Kevin McPhillips*
6+
17
* Removed the unused methods `ActiveRecord::Base.connection_id` and
28
`ActiveRecord::Base.connection_id=`
39

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,8 @@ def attributes
279279
# Returns an <tt>#inspect</tt>-like string for the value of the
280280
# attribute +attr_name+. String attributes are truncated up to 50
281281
# characters, Date and Time attributes are returned in the
282-
# <tt>:db</tt> format, Array attributes are truncated up to 10 values.
283-
# Other attributes return the value of <tt>#inspect</tt> without
284-
# modification.
282+
# <tt>:db</tt> format. Other attributes return the value of
283+
# <tt>#inspect</tt> without modification.
285284
#
286285
# person = Person.create!(name: 'David Heinemeier Hansson ' * 3)
287286
#
@@ -292,17 +291,14 @@ def attributes
292291
# # => "\"2012-10-22 00:15:07\""
293292
#
294293
# person.attribute_for_inspect(:tag_ids)
295-
# # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
294+
# # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]"
296295
def attribute_for_inspect(attr_name)
297296
value = read_attribute(attr_name)
298297

299298
if value.is_a?(String) && value.length > 50
300299
"#{value[0, 50]}...".inspect
301300
elsif value.is_a?(Date) || value.is_a?(Time)
302301
%("#{value.to_s(:db)}")
303-
elsif value.is_a?(Array) && value.size > 10
304-
inspected = value.first(10).inspect
305-
%(#{inspected[0...-1]}, ...])
306302
else
307303
value.inspect
308304
end

activerecord/test/cases/adapters/postgresql/array_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ def test_insert_fixture
189189
end
190190

191191
def test_attribute_for_inspect_for_array_field
192+
record = PgArray.new { |a| a.ratings = (1..10).to_a }
193+
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", record.attribute_for_inspect(:ratings))
194+
end
195+
196+
def test_attribute_for_inspect_for_array_field_for_large_array
192197
record = PgArray.new { |a| a.ratings = (1..11).to_a }
193-
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]", record.attribute_for_inspect(:ratings))
198+
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]", record.attribute_for_inspect(:ratings))
194199
end
195200

196201
def test_escaping

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,33 @@ def setup
2727
ActiveRecord::Base.send(:attribute_method_matchers).concat(@old_matchers)
2828
end
2929

30-
def test_attribute_for_inspect
30+
def test_attribute_for_inspect_string
3131
t = topics(:first)
3232
t.title = "The First Topic Now Has A Title With\nNewlines And More Than 50 Characters"
3333

34-
assert_equal %("#{t.written_on.to_s(:db)}"), t.attribute_for_inspect(:written_on)
3534
assert_equal '"The First Topic Now Has A Title With\nNewlines And ..."', t.attribute_for_inspect(:title)
3635
end
3736

37+
def test_attribute_for_inspect_date
38+
t = topics(:first)
39+
40+
assert_equal %("#{t.written_on.to_s(:db)}"), t.attribute_for_inspect(:written_on)
41+
end
42+
43+
def test_attribute_for_inspect_array
44+
t = topics(:first)
45+
t.content = [Object.new]
46+
47+
assert_match %r(\[#<Object:0x[0-9a-f]+>\]), t.attribute_for_inspect(:content)
48+
end
49+
50+
def test_attribute_for_inspect_long_array
51+
t = topics(:first)
52+
t.content = (1..11).to_a
53+
54+
assert_equal "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]", t.attribute_for_inspect(:content)
55+
end
56+
3857
def test_attribute_present
3958
t = Topic.new
4059
t.title = "hello there!"

0 commit comments

Comments
 (0)