Skip to content

Commit 09fc6ed

Browse files
authored
Merge pull request rails#25577 from sgrif/sg-backport-3055f59e0ad7d5f0eee24c22a45fbae4a40b056e
Backport 3055f59
2 parents 2c07f59 + 6e0cda8 commit 09fc6ed

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
* Ensure hashes can be assigned to attributes created using `composed_of`.
28
Fixes #25210.
39

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,8 @@ def attributes
287287
# Returns an <tt>#inspect</tt>-like string for the value of the
288288
# attribute +attr_name+. String attributes are truncated up to 50
289289
# characters, Date and Time attributes are returned in the
290-
# <tt>:db</tt> format, Array attributes are truncated up to 10 values.
291-
# Other attributes return the value of <tt>#inspect</tt> without
292-
# modification.
290+
# <tt>:db</tt> format. Other attributes return the value of
291+
# <tt>#inspect</tt> without modification.
293292
#
294293
# person = Person.create!(name: 'David Heinemeier Hansson ' * 3)
295294
#
@@ -300,17 +299,14 @@ def attributes
300299
# # => "\"2012-10-22 00:15:07\""
301300
#
302301
# person.attribute_for_inspect(:tag_ids)
303-
# # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
302+
# # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]"
304303
def attribute_for_inspect(attr_name)
305304
value = read_attribute(attr_name)
306305

307306
if value.is_a?(String) && value.length > 50
308307
"#{value[0, 50]}...".inspect
309308
elsif value.is_a?(Date) || value.is_a?(Time)
310309
%("#{value.to_s(:db)}")
311-
elsif value.is_a?(Array) && value.size > 10
312-
inspected = value.first(10).inspect
313-
%(#{inspected[0...-1]}, ...])
314310
else
315311
value.inspect
316312
end

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ def test_insert_fixture
183183
end
184184

185185
def test_attribute_for_inspect_for_array_field
186+
record = PgArray.new { |a| a.ratings = (1..10).to_a }
187+
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", record.attribute_for_inspect(:ratings))
188+
end
189+
190+
def test_attribute_for_inspect_for_array_field_for_large_array
186191
record = PgArray.new { |a| a.ratings = (1..11).to_a }
187-
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]", record.attribute_for_inspect(:ratings))
192+
assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]", record.attribute_for_inspect(:ratings))
188193
end
189194

190195
def test_escaping

activerecord/test/cases/attribute_methods_test.rb

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

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

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

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

0 commit comments

Comments
 (0)