Class: TokenizeAttr::Tokenizer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tokenize_attr/tokenizer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal class that holds all token-generation logic.

Keeping this logic here — rather than inside +TokenizeAttr::Concern+'s class_methods block — ensures that none of these methods are mixed into the including model class, eliminating any risk of method-name collisions on user models.

Class Method Summary collapse

Class Method Details

.apply(klass, attribute, generator:, size:, prefix:, retries:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines which generation strategy to use and configures klass.

Routes to has_secure_token when all of the following are true:

  • generator is nil
  • prefix is nil
  • klass responds to has_secure_token (Rails 5+)

Otherwise installs a custom before_create callback.

Parameters:

  • klass (Class) —

    the model class to configure

  • attribute (Symbol) —

    the attribute to assign the token to

  • generator (Proc, nil) —

    optional callable invoked as generator.call(size) to produce the random portion

  • size (Integer) —

    length passed to the generator or to SecureRandom.base58

  • prefix (String, nil) —

    optional prefix, joined as "prefix-<token>"

  • retries (Integer) —

    max uniqueness-check attempts; ignored on the has_secure_token path



33
34
35
36
37
38
39
40
# File 'lib/tokenize_attr/tokenizer.rb', line 33

def apply(klass, attribute, generator:, size:, prefix:, retries:) # rubocop:disable Metrics/ParameterLists
  if generator.nil? && prefix.nil? && has_secure_token?(klass)
    via_has_secure_token(klass, attribute, size)
  else
    via_callback(klass, attribute, size: size, prefix: prefix,
                                   retries: retries, generator: generator)
  end
end