diff options
author | Ethan <ethan@unth> | 2015-01-12 15:11:23 -0800 |
---|---|---|
committer | Ethan <ethan@unth> | 2015-01-12 15:17:32 -0800 |
commit | f5c2bf10979a3e0d5f33665660c06423b967e4ec (patch) | |
tree | da2f99977210aeaa969b85ebd0ec7bfff9b52140 /activerecord | |
parent | 3adb05565c7de3ba9dd4117b8a3a631a0b238b8c (diff) | |
download | rails-f5c2bf10979a3e0d5f33665660c06423b967e4ec.tar.gz rails-f5c2bf10979a3e0d5f33665660c06423b967e4ec.tar.bz2 rails-f5c2bf10979a3e0d5f33665660c06423b967e4ec.zip |
pretty_print will use #inspect if a subclass redefines it
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/core.rb | 32 | ||||
-rw-r--r-- | activerecord/test/cases/core_test.rb | 11 |
2 files changed, 29 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index a7aff9f724..a5eb18246f 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -456,22 +456,26 @@ module ActiveRecord # Takes a PP and prettily prints this record to it, allowing you to get a nice result from `pp record` # when pp is required. def pretty_print(pp) - pp.object_address_group(self) do - if defined?(@attributes) && @attributes - column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? } - pp.seplist(column_names, proc { pp.text ',' }) do |column_name| - column_value = read_attribute(column_name) - pp.breakable ' ' - pp.group(1) do - pp.text column_name - pp.text ':' - pp.breakable - pp.pp column_value + if self.class.instance_method(:inspect).owner != ActiveRecord::Base.instance_method(:inspect).owner + pp.text inspect + else + pp.object_address_group(self) do + if defined?(@attributes) && @attributes + column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? } + pp.seplist(column_names, proc { pp.text ',' }) do |column_name| + column_value = read_attribute(column_name) + pp.breakable ' ' + pp.group(1) do + pp.text column_name + pp.text ':' + pp.breakable + pp.pp column_value + end end + else + pp.breakable ' ' + pp.text 'not initialized' end - else - pp.breakable ' ' - pp.text 'not initialized' end end end diff --git a/activerecord/test/cases/core_test.rb b/activerecord/test/cases/core_test.rb index 715d92af99..3cb98832c5 100644 --- a/activerecord/test/cases/core_test.rb +++ b/activerecord/test/cases/core_test.rb @@ -98,4 +98,15 @@ class CoreTest < ActiveRecord::TestCase assert actual.start_with?(expected.split('XXXXXX').first) assert actual.end_with?(expected.split('XXXXXX').last) 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 |