Skip to content

PostgreSQL array columns with normalization #1542

@arBmind

Description

@arBmind

Columns that define a normalization like:

normalizes :nos, with: ->(value) { value&.reject(&:nil?)&.uniq&.sort }

Are not detected by paper_trail to contain arrays. As the type is wrapped with a ActiveRecord::Normalization::NormalizedValueType.

My Workaround:

PaperTrail::AttributeSerializers::AttributeSerializerFactory.redefine_singleton_method :ar_pg_array? do |obj|
  if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array)
    if obj.respond_to?(:cast_type)
      obj.cast_type.instance_of?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array)
    else
      obj.instance_of?(::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array)
    end
  else
    false
  end
end

If paper_trail fails to detect the array, the serialization becomes a hash with a values key that contains the array itself. But later fails do deserialize the json to a proper array.

Would it be better to use columns_hash[name].array to detect arrays and store use a noop-array-serializer, that is independent of Postgres?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions