Skip to content

Instantly share code, notes, and snippets.

@jwilger
Created September 24, 2019 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilger/ff745afbefa24f31ba592f2f2a3d38d8 to your computer and use it in GitHub Desktop.
Save jwilger/ff745afbefa24f31ba592f2f2a3d38d8 to your computer and use it in GitHub Desktop.
Compressed Event Data
class Event < ApplicationRecord
attribute :event_data, :gzip_string
end
class GzipStringType < ActiveRecord::Type::Binary
def serialize(value)
return super if value.nil?
super ActiveSupport::Gzip.compress(value.to_s)
end
def deserialize(value)
super decompress(value)
end
private
def decompress(value)
case value
when NilClass
nil
when ActiveModel::Type::Binary::Data
ActiveSupport::Gzip.decompress(value.to_s)
else
ActiveSupport::Gzip.decompress(PG::Connection.unescape_bytea(value))
end
end
end
ActiveRecord::Type.register(:gzip_string, GzipStringType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment