Sha256: 7632ca59add0accd15afada9b59745dd41712fb82be3bd793a53bee02832b1ec

Contents?: true

Size: 820 Bytes

Versions: 7

Compression:

Stored size: 820 Bytes

Contents

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

module ActiveStorage::Patches::ActiveRecord
  class MinimumLengthError < StandardError; end
  MINIMUM_TOKEN_LENGTH = 24

  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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activestorage_legacy-0.2.1 lib/active_storage/patches/active_record.rb
activestorage_legacy-0.2.0 lib/active_storage/patches/active_record.rb
activestorage_legacy-0.1.3 lib/active_storage/patches/active_record.rb
activestorage_legacy-0.1.2 lib/active_storage/patches/active_record.rb
activestorage_legacy-0.1.1 lib/active_storage/patches/active_record.rb
activestorage_legacy-0.1.1.alpha lib/active_storage/patches/active_record.rb
activestorage_legacy-0.1 lib/active_storage/patches/active_record.rb