module ActiveRecord::HasToken

Constants

VERSION

Public Instance Methods

generate_token(attribute, length) click to toggle source
# File lib/activerecord-has_token.rb, line 24
def generate_token(attribute, length)
  random_token = nil
  loop do
    random_token = SecureRandom.alphanumeric(length)
    break unless self.where(attribute => random_token).exists?
  end
  random_token
end
has_token(options = {}) click to toggle source
# File lib/activerecord-has_token.rb, line 8
def has_token(options = {})
  attribute = options[:token_attribute] || :token
  length = options[:length] || 6
  
  define_method("regenerate_#{attribute}") do
    token = self.class.generate_token(attribute, length)

    update_column(attribute, token)
    token
  end

  before_create do 
    self.send("#{attribute}=", self.class.generate_token(attribute, length)) unless self.send("#{attribute}?")
  end
end