Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions lib/active_type/virtual_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,34 @@ def self.deep_dup(hash)
result
end

def self.attribute_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_formatted_s(:db)}")
elsif value.is_a?(Array) && value.size > 10
inspected = value.first(10).inspect
%(#{inspected[0...-1]}, ...])
else
value.inspect
end
end

extend ActiveSupport::Concern

included do
include ActiveType::VirtualAttributes::Serialization
class_attribute :virtual_columns_hash
self.virtual_columns_hash = {}

if respond_to?(:attributes_for_inspect)
# 7.1 had [:id] as a default, we want a consistent default across all versions
self.attributes_for_inspect = :all
else
# ActiveRecord < 7.2
class_attribute :attributes_for_inspect, instance_accessor: false, default: :all
end

class << self
if method_defined?(:attribute)
alias_method :ar_attribute, :attribute
Expand Down Expand Up @@ -270,25 +291,28 @@ def write_virtual_attribute(name, value)
virtual_attributes[name] = value
end

# Returns the contents of the record as a nicely formatted string.
def attributes_for_inspect
self.class.attributes_for_inspect == :all ? attributes.keys : self.class.attributes_for_inspect
end

def inspect
inspection = attributes.collect do |name, value|
"#{name}: #{VirtualAttributes.attribute_for_inspect(value)}"
end.sort.compact.join(", ")
"#<#{self.class} #{inspection}>"
inspect_with_attributes(attributes_for_inspect)
end

def self.attribute_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_formatted_s(:db)}")
elsif value.is_a?(Array) && value.size > 10
inspected = value.first(10).inspect
%(#{inspected[0...-1]}, ...])
else
value.inspect
end
def full_inspect
inspect_with_attributes(attributes.keys)
end

# Returns the contents of the record as a nicely formatted string.
def inspect_with_attributes(attribute_names)
inspection = attribute_names.collect do |name|
name = name.to_s
if attributes.key?(name)
value = attributes[name]
"#{name}: #{VirtualAttributes.attribute_for_inspect(value)}"
end
end.compact.sort.join(", ")
"#<#{self.class} #{inspection}>"
end

private
Expand Down
25 changes: 24 additions & 1 deletion spec/active_type/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ class ObjectWithUnsupportedTypes < Object
attribute :virtual_hash, :hash
end

class ObjectWithClassInspectFilter < ActiveType::Object
attribute :visible, :string
attribute :hidden, :string

self.attributes_for_inspect = [:visible]
end
end


Expand Down Expand Up @@ -246,7 +252,6 @@ class ObjectWithUnsupportedTypes < Object
end

describe '#inspect' do

it 'returns the contents of the object as a nicely formatted string' do
t = Time.now
subject.virtual_string = "string"
Expand All @@ -258,6 +263,24 @@ class ObjectWithUnsupportedTypes < Object
expect(subject.inspect).to eq("#<ObjectSpec::Object virtual_attribute: nil, virtual_boolean: true, virtual_date: \"#{Date.today}\", virtual_integer: 17, virtual_string: \"string\", virtual_time: \"#{t.to_formatted_s(:db)}\", virtual_type_attribute: nil>")
end

it 'does not filter out any attributes per default' do
object = ObjectSpec::ObjectWithClassInspectFilter.new(visible: 'seen', hidden: 'also-seen')
expect(object.full_inspect).to eq('#<ObjectSpec::ObjectWithClassInspectFilter hidden: "also-seen", visible: "seen">')
end

context 'with attributes_for_inspect class attribute' do
it 'filters attributes based on the class configuration' do
object = ObjectSpec::ObjectWithClassInspectFilter.new(visible: 'seen', hidden: 'invisible')
expect(object.inspect).to eq('#<ObjectSpec::ObjectWithClassInspectFilter visible: "seen">')
end
end
end

describe '#full_inspect' do
it 'shows all attributes' do
object = ObjectSpec::ObjectWithClassInspectFilter.new(visible: 'seen', hidden: 'not-so-invisible-anymore')
expect(object.full_inspect).to eq('#<ObjectSpec::ObjectWithClassInspectFilter hidden: "not-so-invisible-anymore", visible: "seen">')
end
end

describe '#attributes' do
Expand Down