Module: HasTokenId::Concern::ClassMethods

Defined in:
lib/has_token_id/concern.rb

Instance Method Summary collapse

Instance Method Details

#default_token_optionsObject

Default options as well as an overwrite point so you can assign different defaults to different models



16
17
18
19
20
21
22
# File 'lib/has_token_id/concern.rb', line 16

def default_token_options
  @default_token_options ||= begin
    options = HasTokenId.default_token_options
    options[:prefix] ||= self.name[0, 1]
    options
  end
end

#generate_unique_tokenObject

Generates a unique token based on the options



25
26
27
28
29
30
31
32
33
34
# File 'lib/has_token_id/concern.rb', line 25

def generate_unique_token
  record, options = true, @has_token_id_options
  conditions = {}
  while record
    token = [ options[:prefix], Digest::SHA1.hexdigest((Time.now.to_i * rand()).to_s)].compact.join[0...options[:length].to_i]
    conditions[options[:param_name].to_sym] = token
    record = self.where(conditions).first
  end
  token
end