Skip to content

Commit e46e56b

Browse files
authored
Merge pull request rails#51892 from florin555/fix_pretty_print
Make `pretty_print` behave more similar to `inspect`.
2 parents 93f4b6d + b5207ec commit e46e56b

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,8 @@ def pretty_print(pp)
748748
pp.text attr_name
749749
pp.text ":"
750750
pp.breakable
751-
value = _read_attribute(attr_name)
752-
value = inspection_filter.filter_param(attr_name, value) unless value.nil?
753-
pp.pp value
751+
value = attribute_for_inspect(attr_name)
752+
pp.text value
754753
end
755754
end
756755
else

activerecord/test/cases/core_test.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ def test_inspect_relation_with_virtual_field
7878
assert_match(/virtual_field: 1/, relation.first.full_inspect)
7979
end
8080

81+
def test_inspect_with_overridden_attribute_for_inspect
82+
topic = topics(:first)
83+
84+
topic.instance_eval do
85+
def attribute_for_inspect(attr_name)
86+
if attr_name == "title"
87+
title.upcase.inspect
88+
else
89+
super
90+
end
91+
end
92+
end
93+
94+
assert_match(/title: "THE FIRST TOPIC"/, topic.full_inspect)
95+
end
96+
8197
def test_full_inspect_lists_all_attributes
8298
topic = topics(:first)
8399

@@ -118,9 +134,9 @@ def test_pretty_print_full
118134
title: "The First Topic",
119135
author_name: "David",
120136
author_email_address: "[email protected]",
121-
written_on: 2003-07-16 14:28:11(?:\.2233)? UTC,
122-
bonus_time: 2000-01-01 14:28:00 UTC,
123-
last_read: Thu, 15 Apr 2004,
137+
written_on: "2003-07-16 14:28:11\\.223300000 \\+0000",
138+
bonus_time: "2000-01-01 14:28:00\\.000000000 \\+0000",
139+
last_read: "2004-04-15",
124140
content: "Have a nice day",
125141
important: nil,
126142
binary_content: nil,
@@ -165,6 +181,26 @@ def test_pretty_print_with_non_primary_key_id_attribute
165181
assert_match(/id: 1/, actual)
166182
end
167183

184+
def test_pretty_print_with_overridden_attribute_for_inspect
185+
topic = topics(:first)
186+
187+
topic.instance_eval do
188+
def attribute_for_inspect(attr_name)
189+
if attr_name == "title"
190+
title.upcase.inspect
191+
else
192+
super
193+
end
194+
end
195+
end
196+
197+
Topic.stub(:attributes_for_inspect, :all) do
198+
actual = +""
199+
PP.pp(topic, StringIO.new(actual))
200+
assert_match(/title: "THE FIRST TOPIC"/, actual)
201+
end
202+
end
203+
168204
def test_find_by_cache_does_not_duplicate_entries
169205
Topic.initialize_find_by_cache
170206
using_prepared_statements = Topic.lease_connection.prepared_statements

activerecord/test/cases/filter_attributes_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class FilterAttributesTest < ActiveRecord::TestCase
121121
actual = "".dup
122122
PP.pp(user, StringIO.new(actual))
123123

124-
assert_includes actual, 'name: "[FILTERED]"'
124+
assert_includes actual, "name: [FILTERED]"
125125
assert_equal 1, actual.scan("[FILTERED]").length
126126
end
127127

@@ -141,8 +141,8 @@ class FilterAttributesTest < ActiveRecord::TestCase
141141
actual = "".dup
142142
PP.pp(user, StringIO.new(actual))
143143

144-
assert_includes actual, 'auth_token: "[FILTERED]"'
145-
assert_includes actual, 'token: "[FILTERED]"'
144+
assert_includes actual, "auth_token: [FILTERED]"
145+
assert_includes actual, "token: [FILTERED]"
146146
ensure
147147
User.instance_variable_set(:@filter_attributes, nil)
148148
end

0 commit comments

Comments
 (0)