@@ -134,13 +134,30 @@ def self.deep_dup(hash)
134134 result
135135 end
136136
137+ def self . attribute_for_inspect ( value )
138+ if value . is_a? ( String ) && value . length > 50
139+ "#{ value [ 0 , 50 ] } ..." . inspect
140+ elsif value . is_a? ( Date ) || value . is_a? ( Time )
141+ %("#{ value . to_formatted_s ( :db ) } ")
142+ elsif value . is_a? ( Array ) && value . size > 10
143+ inspected = value . first ( 10 ) . inspect
144+ %(#{ inspected [ 0 ...-1 ] } , ...])
145+ else
146+ value . inspect
147+ end
148+ end
149+
137150 extend ActiveSupport ::Concern
138151
139152 included do
140153 include ActiveType ::VirtualAttributes ::Serialization
141154 class_attribute :virtual_columns_hash
142155 self . virtual_columns_hash = { }
143156
157+ unless respond_to? ( :attributes_for_inspect ) # ActiveRecord < 7.2
158+ class_attribute :attributes_for_inspect , default : :all
159+ end
160+
144161 class << self
145162 if method_defined? ( :attribute )
146163 alias_method :ar_attribute , :attribute
@@ -270,25 +287,28 @@ def write_virtual_attribute(name, value)
270287 virtual_attributes [ name ] = value
271288 end
272289
273- # Returns the contents of the record as a nicely formatted string.
290+ def attributes_for_inspect
291+ self . class . attributes_for_inspect == :all ? attributes . keys : self . class . attributes_for_inspect
292+ end
293+
274294 def inspect
275- inspection = attributes . collect do |name , value |
276- "#{ name } : #{ VirtualAttributes . attribute_for_inspect ( value ) } "
277- end . sort . compact . join ( ", " )
278- "#<#{ self . class } #{ inspection } >"
295+ inspect_with_attributes ( attributes_for_inspect )
279296 end
280297
281- def self . attribute_for_inspect ( value )
282- if value . is_a? ( String ) && value . length > 50
283- "#{ value [ 0 , 50 ] } ..." . inspect
284- elsif value . is_a? ( Date ) || value . is_a? ( Time )
285- %("#{ value . to_formatted_s ( :db ) } ")
286- elsif value . is_a? ( Array ) && value . size > 10
287- inspected = value . first ( 10 ) . inspect
288- %(#{ inspected [ 0 ...-1 ] } , ...])
289- else
290- value . inspect
291- end
298+ def full_inspect
299+ inspect_with_attributes ( attributes . keys )
300+ end
301+
302+ # Returns the contents of the record as a nicely formatted string.
303+ def inspect_with_attributes ( attribute_names )
304+ inspection = attribute_names . collect do |name |
305+ name = name . to_s
306+ if attributes . key? ( name )
307+ value = attributes [ name ]
308+ "#{ name } : #{ VirtualAttributes . attribute_for_inspect ( value ) } "
309+ end
310+ end . compact . sort . join ( ", " )
311+ "#<#{ self . class } #{ inspection } >"
292312 end
293313
294314 private
0 commit comments