Sha256: fbd6113c208077b530ae39ac81c9222d9585288183151737ba96815e3e22e0b5

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module PaperTrail
  module TypeSerializers
    # Provides an alternative method of serialization
    # and deserialization of PostgreSQL array columns.
    class PostgresArraySerializer
      def initialize(subtype, delimiter)
        @subtype = subtype
        @delimiter = delimiter
      end

      def serialize(array)
        return serialize_with_ar(array) if active_record_pre_502?
        array
      end

      def deserialize(array)
        return deserialize_with_ar(array) if active_record_pre_502?

        case array
        # Needed for legacy reasons. If serialized array is a string
        # then it was serialized with Rails < 5.0.2.
        when ::String then deserialize_with_ar(array)
        else array
        end
      end

      private

      def active_record_pre_502?
        ::ActiveRecord::VERSION::MAJOR < 5 ||
          (::ActiveRecord::VERSION::MINOR.zero? && ::ActiveRecord::VERSION::TINY < 2)
      end

      def serialize_with_ar(array)
        ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.
          new(@subtype, @delimiter).
          serialize(array)
      end

      def deserialize_with_ar(array)
        ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.
          new(@subtype, @delimiter).
          deserialize(array)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paper_trail-8.1.2 lib/paper_trail/type_serializers/postgres_array_serializer.rb
paper_trail-8.1.1 lib/paper_trail/type_serializers/postgres_array_serializer.rb