class PaperTrail::AttributeSerializers::ObjectAttribute

Serialize or deserialize the ‘version.object` column.

Public Class Methods

new(model_class) click to toggle source
# File lib/paper_trail/attribute_serializers/object_attribute.rb, line 9
def initialize(model_class)
  @model_class = model_class

  # ActiveRecord since 7.0 has a built-in encryption mechanism
  @encrypted_attributes =
    if PaperTrail.active_record_gte_7_0?
      @model_class.encrypted_attributes&.map(&:to_s)
    end
end

Public Instance Methods

deserialize(attributes) click to toggle source
# File lib/paper_trail/attribute_serializers/object_attribute.rb, line 23
def deserialize(attributes)
  alter(attributes, :deserialize)
end
serialize(attributes) click to toggle source
# File lib/paper_trail/attribute_serializers/object_attribute.rb, line 19
def serialize(attributes)
  alter(attributes, :serialize)
end

Private Instance Methods

alter(attributes, serialization_method) click to toggle source

Modifies ‘attributes` in place. TODO: Return a new hash instead.

# File lib/paper_trail/attribute_serializers/object_attribute.rb, line 31
def alter(attributes, serialization_method)
  # Don't serialize non-encrypted before values before inserting into columns of type
  # `JSON` on `PostgreSQL` databases.
  attributes_to_serialize =
    object_col_is_json? ? attributes.slice(*@encrypted_attributes) : attributes
  return attributes if attributes_to_serialize.blank?

  serializer = CastAttributeSerializer.new(@model_class)
  attributes_to_serialize.each do |key, value|
    attributes[key] = serializer.send(serialization_method, key, value)
  end

  attributes
end
object_col_is_json?() click to toggle source
# File lib/paper_trail/attribute_serializers/object_attribute.rb, line 46
def object_col_is_json?
  @model_class.paper_trail.version_class.object_col_is_json?
end