require "paper_trail/type_serializers/postgres_array_serializer"

module PaperTrail
  module AttributeSerializers
    # Values returned by some Active Record serializers are
    # not suited for writing JSON to a text column. This factory
    # replaces certain default Active Record serializers
    # with custom PaperTrail ones.
    module AttributeSerializerFactory
      def self.for(klass, attr)
        active_record_serializer = klass.type_for_attribute(attr)
        case active_record_serializer
        when ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array then
          TypeSerializers::PostgresArraySerializer.new(
            active_record_serializer.subtype,
            active_record_serializer.delimiter
          )
        else
          active_record_serializer
        end
      end
    end
  end
end

require "paper_trail/type_serializers/postgres_array_serializer"

module PaperTrail
  module AttributeSerializers
    # Values returned by some Active Record serializers are
    # not suited for writing JSON to a text column. This factory
    # replaces certain default Active Record serializers
    # with custom PaperTrail ones.
    module AttributeSerializerFactory
      AR_PG_ARRAY_CLASS = "ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array".freeze

      def self.for(klass, attr)
        active_record_serializer = klass.type_for_attribute(attr)
        if active_record_serializer.class.name == AR_PG_ARRAY_CLASS
          TypeSerializers::PostgresArraySerializer.new(
            active_record_serializer.subtype,
            active_record_serializer.delimiter
          )
        else
          active_record_serializer
        end
      end
    end
  end
end

Fix NameError when pg is not loaded
Fix NameError when pg is not loaded

Most apps only load a single db adapter gem.

A MySQL app, for example will not load the pg gem.

See https://github.com/airblade/paper_trail/issues/1022
