Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compressed fields in Mongoid
require 'zlib'
module Mongoid
module Fields
module Compressed
extend ActiveSupport::Concern
class Field
include Mongoid::Fields::Serializable
def deserialize(object)
Zlib::Inflate.inflate(object.to_s)
end
def serialize(object)
BSON::Binary.new(Zlib::Deflate.deflate(object.to_s))
end
end
module ClassMethods
def compressed_field(name, options = {})
field(name, options.merge(type: Mongoid::Fields::Compressed::Field))
end
end
end
end
end
@jarthod

This comment has been minimized.

Copy link

@jarthod jarthod commented Feb 23, 2019

Hey, thanks for this! I just made a more recent version compatible with Mongoid 6.4 in case anyone's looking for this: https://gist.github.com/jarthod/6c7ddbea1c47b9ca5f159b6c28f81374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment