Make sure that attribute_for_inspect respect filter_attributes
Before this change while inspect was respecting that option, attribute_for_inspect was not. I also changed inspect to use attribute_for_inspect so we make sure the behavior is consistent in the two places and simplified the implementation. Fixes #40725.
This commit is contained in:
parent
9f1623bd34
commit
d04f74dd72
@ -285,7 +285,7 @@ def attribute_for_inspect(attr_name)
|
||||
attr_name = attr_name.to_s
|
||||
attr_name = self.class.attribute_aliases[attr_name] || attr_name
|
||||
value = _read_attribute(attr_name)
|
||||
format_for_inspect(value)
|
||||
format_for_inspect(attr_name, value)
|
||||
end
|
||||
|
||||
# Returns +true+ if the specified +attribute+ has been set by the user or by a
|
||||
@ -407,13 +407,19 @@ def attributes_for_create(attribute_names)
|
||||
end
|
||||
end
|
||||
|
||||
def format_for_inspect(value)
|
||||
if value.is_a?(String) && value.length > 50
|
||||
"#{value[0, 50]}...".inspect
|
||||
elsif value.is_a?(Date) || value.is_a?(Time)
|
||||
%("#{value.to_s(:inspect)}")
|
||||
else
|
||||
def format_for_inspect(name, value)
|
||||
if value.nil?
|
||||
value.inspect
|
||||
else
|
||||
inspected_value = if value.is_a?(String) && value.length > 50
|
||||
"#{value[0, 50]}...".inspect
|
||||
elsif value.is_a?(Date) || value.is_a?(Time)
|
||||
%("#{value.to_s(:inspect)}")
|
||||
else
|
||||
value.inspect
|
||||
end
|
||||
|
||||
inspection_filter.filter_param(name, inspected_value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -668,14 +668,7 @@ def inspect
|
||||
inspection = if defined?(@attributes) && @attributes
|
||||
self.class.attribute_names.collect do |name|
|
||||
if _has_attribute?(name)
|
||||
attr = _read_attribute(name)
|
||||
value = if attr.nil?
|
||||
attr.inspect
|
||||
else
|
||||
attr = format_for_inspect(attr)
|
||||
inspection_filter.filter_param(name, attr)
|
||||
end
|
||||
"#{name}: #{value}"
|
||||
"#{name}: #{attribute_for_inspect(name)}"
|
||||
end
|
||||
end.compact.join(", ")
|
||||
else
|
||||
|
@ -31,7 +31,13 @@ class FilterAttributesTest < ActiveRecord::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "string filter_attributes perform pertial match" do
|
||||
test "filter_attributes affects attribute_for_inspect" do
|
||||
Admin::User.all.each do |user|
|
||||
assert_equal "[FILTERED]", user.attribute_for_inspect(:name)
|
||||
end
|
||||
end
|
||||
|
||||
test "string filter_attributes perform partial match" do
|
||||
ActiveRecord::Base.filter_attributes = ["n"]
|
||||
Admin::Account.all.each do |account|
|
||||
assert_includes account.inspect, "name: [FILTERED]"
|
||||
|
Loading…
Reference in New Issue
Block a user