Module: HasSecureToken::ClassMethods
- Defined in:
- lib/has_secure_token.rb
Instance Method Summary collapse
-
#has_secure_token(*args) ⇒ Object
Example using Active Record (which automatically includes ActiveModel::SecurePassword):.
Instance Method Details
#has_secure_token(*args) ⇒ Object
Example using Active Record (which automatically includes ActiveModel::SecurePassword):
# Schema: User(auth_token:string, invitation_token:string)
class User < ActiveRecord::Base
has_secure_token :auth_token, :invitation_token
end
user = User.new
user.save
user.auth_token # => "44539a6a59835a4ee9d7b112"
user.invitation_token # => "226dd46af6be78953bde1648"
user.regenerate_auth_token # => true
user.regenerate_invitation_token # => true
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/has_secure_token.rb', line 20 def has_secure_token(*args) # Load securerandom only when has_secure_key is used. require 'securerandom' include InstanceMethodsOnActivation cattr_accessor :token_columns, :options = args. key_length = .fetch(:key_length, 24) bytes = (key_length / 2.0).ceil args.each do |attribute| define_method("regenerate_#{attribute}!") do send(:generate_unique_secure_token, attribute, bytes, key_length) save end end before_create do args.each do |attribute| self.generate_unique_secure_token(attribute, bytes, key_length) end end end |