Created
December 24, 2012 13:30
-
-
Save amencarini/4369262 to your computer and use it in GitHub Desktop.
A way to get non-random ids with mongoid based on object characteristics. May be useful for testing purposes (e.g.: mocking Solr indexing with FakeWeb).
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
module DerandomizableId | |
extend ActiveSupport::Concern | |
included do | |
before_create :set_id | |
end | |
def set_id | |
attrs = self.attributes | |
attrs.delete('_id') | |
string = Digest::SHA1.hexdigest(attrs.to_s)[1..24] | |
self._id = Moped::BSON::ObjectId.from_string(string) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment