Sha256: 3cde0b8efdaf3012349d7d391c22e5b4926bdde0ab5ff26e896f79df67565207

Contents?: true

Size: 1.1 KB

Versions: 22

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "paper_trail/attribute_serializers/cast_attribute_serializer"

module PaperTrail
  module AttributeSerializers
    # Serialize or deserialize the `version.object` column.
    class ObjectAttribute
      def initialize(model_class)
        @model_class = model_class
      end

      def serialize(attributes)
        alter(attributes, :serialize)
      end

      def deserialize(attributes)
        alter(attributes, :deserialize)
      end

      private

      # Modifies `attributes` in place.
      # TODO: Return a new hash instead.
      def alter(attributes, serialization_method)
        # Don't serialize before values before inserting into columns of type
        # `JSON` on `PostgreSQL` databases.
        return attributes if object_col_is_json?

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

      def object_col_is_json?
        @model_class.paper_trail.version_class.object_col_is_json?
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/paper_trail-12.3.0/lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-14.0.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-13.0.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-12.3.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-12.2.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-12.1.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-12.0.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-11.1.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-11.0.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.3.1 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.3.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.2.1 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.2.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.1.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.0.1 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-10.0.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-9.2.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-9.1.1 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-9.1.0 lib/paper_trail/attribute_serializers/object_attribute.rb
paper_trail-9.0.2 lib/paper_trail/attribute_serializers/object_attribute.rb