Skip to content

Instantly share code, notes, and snippets.

@ace-subido
Created May 2, 2014 15:15
Show Gist options
  • Save ace-subido/286174e9d8376017cc3c to your computer and use it in GitHub Desktop.
Save ace-subido/286174e9d8376017cc3c to your computer and use it in GitHub Desktop.
Tokenable Ruby on Rails Models
module Tokenable
extend ActiveSupport::Concern
included do
before_create :generate_token
end
protected
def generate_token
self.code = loop do
random_token = SecureRandom.urlsafe_base64(nil, false)
break random_token unless self.class.exists?(code: random_token)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment