1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #18474 from notEthan/pretty_print_inspect

pretty_print will use #inspect if a subclass redefines it
This commit is contained in:
Sean Griffin 2015-01-23 16:25:15 -07:00
commit 847395a04d
2 changed files with 16 additions and 0 deletions

View file

@ -456,6 +456,7 @@ module ActiveRecord
# Takes a PP and prettily prints this record to it, allowing you to get a nice result from `pp record` # Takes a PP and prettily prints this record to it, allowing you to get a nice result from `pp record`
# when pp is required. # when pp is required.
def pretty_print(pp) def pretty_print(pp)
return super if custom_inspect_method_defined?
pp.object_address_group(self) do pp.object_address_group(self) do
if defined?(@attributes) && @attributes if defined?(@attributes) && @attributes
column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? } column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? }
@ -560,5 +561,9 @@ module ActiveRecord
@attributes = @attributes.dup @attributes = @attributes.dup
end end
end end
def custom_inspect_method_defined?
self.class.instance_method(:inspect).owner != ActiveRecord::Base.instance_method(:inspect).owner
end
end end
end end

View file

@ -98,4 +98,15 @@ class CoreTest < ActiveRecord::TestCase
assert actual.start_with?(expected.split('XXXXXX').first) assert actual.start_with?(expected.split('XXXXXX').first)
assert actual.end_with?(expected.split('XXXXXX').last) assert actual.end_with?(expected.split('XXXXXX').last)
end end
def test_pretty_print_overridden_by_inspect
subtopic = Class.new(Topic) do
def inspect
"inspecting topic"
end
end
actual = ''
PP.pp(subtopic.new, StringIO.new(actual))
assert_equal "inspecting topic\n", actual
end
end end