Created
August 12, 2018 21:47
-
-
Save ZephiroRB/487edbf9849b006de72c9fa6e90b4690 to your computer and use it in GitHub Desktop.
Concerns Tokenable Models
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 Tokenable | |
extend ActiveSupport::Concern | |
included do | |
before_create :generate_token | |
scope :search_token, ->(token) { find_by(token: token, disable: false ) } | |
def to_param | |
token | |
end | |
end | |
protected | |
def generate_token | |
self.token = loop do | |
random_token = SecureRandom.urlsafe_base64(10, false) | |
break random_token unless self.class.exists?(token: random_token) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment