Sha256: 343be83ae945d34794de6ec3aa40c1f32b5bc602241025450cc12eee83c2abea
Contents?: true
Size: 737 Bytes
Versions: 1
Compression:
Stored size: 737 Bytes
Contents
module HasRandomToken extend ActiveSupport::Concern module ClassMethods def has_random_token(attribute: :token, unique: false, length: 32) require 'securerandom' define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_random_token(attribute, unique, length) } before_create { self.send("#{attribute}=", self.class.generate_random_token(attribute, unique, length)) unless self.send("#{attribute}?")} end def generate_random_token(attribute, unique, length) begin token = SecureRandom.base64(length)[0 .. length - 1] end while unique && self.exists?(attribute => token) return token end end end ActiveRecord::Base.send :include, HasRandomToken
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
has_random_token-0.1.0 | lib/has_random_token.rb |