Skip to content

Commit 863cb00

Browse files
authored
Merge pull request rails#52801 from Shopify/fix-attributes-for-inspect
Set `attributes_for_inspect` to `:all` in the console
2 parents 48f589f + a2dfcb0 commit 863cb00

File tree

6 files changed

+57
-40
lines changed

6 files changed

+57
-40
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def self.configurations
104104
class_attribute :shard_selector, instance_accessor: false, default: nil
105105

106106
# Specifies the attributes that will be included in the output of the #inspect method
107-
class_attribute :attributes_for_inspect, instance_accessor: false, default: [:id]
107+
class_attribute :attributes_for_inspect, instance_accessor: false, default: :all
108108

109109
def self.application_record_class? # :nodoc:
110110
if ActiveRecord.application_record_class
@@ -732,7 +732,7 @@ def inspect
732732

733733
# Returns the full contents of the record as a nicely formatted string.
734734
def full_inspect
735-
inspect_with_attributes(attribute_names)
735+
inspect_with_attributes(all_attributes_for_inspect)
736736
end
737737

738738
# Takes a PP and prettily prints this record to it, allowing you to get a nice result from <tt>pp record</tt>
@@ -824,7 +824,13 @@ def inspect_with_attributes(attributes_to_list)
824824
end
825825

826826
def attributes_for_inspect
827-
self.class.attributes_for_inspect == :all ? attribute_names : self.class.attributes_for_inspect
827+
self.class.attributes_for_inspect == :all ? all_attributes_for_inspect : self.class.attributes_for_inspect
828+
end
829+
830+
def all_attributes_for_inspect
831+
return [] unless @attributes
832+
833+
attribute_names
828834
end
829835
end
830836
end

activerecord/lib/active_record/railtie.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Railtie < Rails::Railtie # :nodoc:
6969
Rails.logger.broadcast_to(console)
7070
end
7171
ActiveRecord.verbose_query_logs = false
72+
ActiveRecord::Base.attributes_for_inspect = :all if Rails.env.production?
7273
end
7374

7475
runner do
@@ -435,15 +436,5 @@ class Railtie < Rails::Railtie # :nodoc:
435436
end
436437
end
437438
end
438-
439-
initializer "active_record.attributes_for_inspect" do |app|
440-
ActiveSupport.on_load(:active_record) do
441-
if app.config.consider_all_requests_local
442-
if app.config.active_record.attributes_for_inspect.nil?
443-
ActiveRecord::Base.attributes_for_inspect = :all
444-
end
445-
end
446-
end
447-
end
448439
end
449440
end

activerecord/test/cases/core_test.rb

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def test_inspect_class
1717
assert_match(/^Topic\(id: integer, title: string/, Topic.inspect)
1818
end
1919

20-
def test_inspect_instance_includes_just_id_by_default
20+
def test_inspect_instance
2121
topic = topics(:first)
22-
assert_equal %(#<Topic id: 1>), topic.inspect
22+
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "[email protected]", written_on: "#{topic.written_on.to_fs(:inspect)}", bonus_time: "#{topic.bonus_time.to_fs(:inspect)}", last_read: "#{topic.last_read.to_fs(:inspect)}", content: "Have a nice day", important: nil, binary_content: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_fs(:inspect)}", updated_at: "#{topic.updated_at.to_fs(:inspect)}">), topic.inspect
2323
end
2424

2525
def test_inspect_includes_attributes_from_attributes_for_inspect
@@ -107,7 +107,26 @@ def test_pretty_print_new
107107
actual = +""
108108
PP.pp(topic, StringIO.new(actual))
109109
expected = <<~PRETTY
110-
#<Topic:0xXXXXXX id: nil>
110+
#<Topic:0xXXXXXX
111+
id: nil,
112+
title: nil,
113+
author_name: nil,
114+
author_email_address: "[email protected]",
115+
written_on: nil,
116+
bonus_time: nil,
117+
last_read: nil,
118+
content: nil,
119+
important: nil,
120+
binary_content: nil,
121+
approved: true,
122+
replies_count: 0,
123+
unique_replies_count: 0,
124+
parent_id: nil,
125+
parent_title: nil,
126+
type: nil,
127+
group: nil,
128+
created_at: nil,
129+
updated_at: nil>
111130
PRETTY
112131
assert actual.start_with?(expected.split("XXXXXX").first)
113132
assert actual.end_with?(expected.split("XXXXXX").last)
@@ -118,7 +137,26 @@ def test_pretty_print_persisted
118137
actual = +""
119138
PP.pp(topic, StringIO.new(actual))
120139
expected = <<~PRETTY
121-
#<Topic:0x\\w+ id: 1>
140+
#<Topic:0x\\w+
141+
id: 1,
142+
title: "The First Topic",
143+
author_name: "David",
144+
author_email_address: "[email protected]",
145+
written_on: "2003-07-16 14:28:11\\.223300000 \\+0000",
146+
bonus_time: "2000-01-01 14:28:00\\.000000000 \\+0000",
147+
last_read: "2004-04-15",
148+
content: "Have a nice day",
149+
important: nil,
150+
binary_content: nil,
151+
approved: false,
152+
replies_count: 1,
153+
unique_replies_count: 0,
154+
parent_id: nil,
155+
parent_title: nil,
156+
type: nil,
157+
group: nil,
158+
created_at: [^,]+,
159+
updated_at: [^,>]+>
122160
PRETTY
123161
assert_match(/\A#{expected}\z/, actual)
124162
end

activerecord/test/cases/filter_attributes_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ class FilterAttributesTest < ActiveRecord::TestCase
1111
fixtures :"admin/users", :"admin/accounts"
1212

1313
setup do
14-
@previous_attributes_for_inspect = ActiveRecord::Base.attributes_for_inspect
15-
ActiveRecord::Base.attributes_for_inspect = :all
1614
@previous_filter_attributes = ActiveRecord::Base.filter_attributes
1715
ActiveRecord::Base.filter_attributes = [:name]
1816
ActiveRecord.use_yaml_unsafe_load = true
1917
end
2018

2119
teardown do
22-
ActiveRecord::Base.attributes_for_inspect = @previous_attributes_for_inspect
2320
ActiveRecord::Base.filter_attributes = @previous_filter_attributes
2421
end
2522

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ Rails.application.configure do
109109

110110
# Do not dump schema after migrations.
111111
config.active_record.dump_schema_after_migration = false
112+
113+
# Only use :id for inspections in production.
114+
config.active_record.attributes_for_inspect = [:id]
112115
<%- end -%>
113116

114117
# Enable DNS rebinding protection and other `Host` header attacks.

railties/test/application/configuration_test.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,33 +4728,15 @@ class Post < ActiveRecord::Base
47284728
assert_equal(:html4, Rails.application.config.dom_testing_default_html_version)
47294729
end
47304730

4731-
test "sets ActiveRecord::Base.attributes_for_inspect to [:id] when config.consider_all_requests_local = false" do
4732-
add_to_config "config.consider_all_requests_local = false"
4733-
4734-
app "production"
4735-
4736-
assert_equal [:id], ActiveRecord::Base.attributes_for_inspect
4737-
end
4738-
4739-
test "sets ActiveRecord::Base.attributes_for_inspect to :all when config.consider_all_requests_local = true" do
4740-
add_to_config "config.consider_all_requests_local = true"
4741-
4742-
app "development"
4743-
4744-
assert_equal :all, ActiveRecord::Base.attributes_for_inspect
4745-
end
4746-
4747-
test "app configuration takes precedence over default" do
4748-
add_to_config "config.consider_all_requests_local = true"
4731+
test "app attributes_for_inspect configuration takes precedence over default" do
47494732
add_to_config "config.active_record.attributes_for_inspect = [:foo]"
47504733

47514734
app "development"
47524735

47534736
assert_equal [:foo], ActiveRecord::Base.attributes_for_inspect
47544737
end
47554738

4756-
test "model's configuration takes precedence over default" do
4757-
add_to_config "config.consider_all_requests_local = true"
4739+
test "model's attributes_for_inspect configuration takes precedence over default" do
47584740
app_file "app/models/foo.rb", <<-RUBY
47594741
class Foo < ApplicationRecord
47604742
self.attributes_for_inspect = [:foo]

0 commit comments

Comments
 (0)