Skip to content

Instantly share code, notes, and snippets.

@inre
Created December 10, 2008 18:59
Show Gist options
  • Save inre/34440 to your computer and use it in GitHub Desktop.
Save inre/34440 to your computer and use it in GitHub Desktop.
def generate_token(length=8)
alphanumerics = ('a'..'z').to_a.concat(('A'..'Z').to_a.concat(('0'..'9').to_a))
self.token = alphanumerics.sort_by{rand}.to_s[0..length]
# Ensure uniqueness of the token..
generate_token unless Widget.find_by_token(self.token).nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment