Skip to content

Instantly share code, notes, and snippets.

@amencarini
Created December 24, 2012 13:30
Show Gist options
  • Save amencarini/4369262 to your computer and use it in GitHub Desktop.
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).
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