Module: ActiveStorage::Patches::ActiveRecord

Defined in:
lib/active_storage/patches/active_record.rb

Overview

Provides the class-level DSL for declaring that an Active Record model has attached blobs.

Defined Under Namespace

Classes: MinimumLengthError

Constant Summary collapse

MINIMUM_TOKEN_LENGTH =
24

Instance Method Summary collapse

Instance Method Details

#generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH) ⇒ Object



16
17
18
# File 'lib/active_storage/patches/active_record.rb', line 16

def generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
  SecureRandom.base58(length)
end

#has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/active_storage/patches/active_record.rb', line 7

def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH)
  if length < MINIMUM_TOKEN_LENGTH
    raise MinimumLengthError, "Token requires a minimum length of #{MINIMUM_TOKEN_LENGTH} characters."
  end

  define_method("regenerate_#{attribute}") { update_attributes! attribute => self.class.generate_unique_secure_token(length: length) }
  before_create { send("#{attribute}=", self.class.generate_unique_secure_token(length: length)) unless send("#{attribute}?") }
end