Created
October 11, 2012 05:38
-
-
Save joshed-io/3870418 to your computer and use it in GitHub Desktop.
Safer Paperclip id_partition interpolation for mongoid
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
# Googling for how to implement a good Paperclip id_partition for mongoid, you might find - | |
# attachment.instance.id.to_s.scan(/.{4}/).join("/") | |
# There are problems with this. Because mongoid ID's are hex strings, a 4-character string can contain | |
# 4^16 or 65536 values. Usually with id partition we're trying to make sure no directory ever holds more | |
# than 32k files, so 64k is potentially dangerous. To be safe we need to drop to 3 characters or 4096 values. | |
Paperclip.interpolates :id_partition do |attachment, style| | |
attachment.instance.id.to_s.scan(/.{3}/).join("/") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment