Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created October 15, 2012 13:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romanbsd/3892391 to your computer and use it in GitHub Desktop.
Save romanbsd/3892391 to your computer and use it in GitHub Desktop.
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
Copy link

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