Created
September 24, 2019 01:55
-
-
Save jwilger/ff745afbefa24f31ba592f2f2a3d38d8 to your computer and use it in GitHub Desktop.
Compressed Event Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event < ApplicationRecord | |
attribute :event_data, :gzip_string | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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