Sha256: c6d33eb92794bfe7b4cf2229081b7cb19004d40239b3a0d20b26df07902b96cf

Contents?: true

Size: 702 Bytes

Versions: 1

Compression:

Stored size: 702 Bytes

Contents

# frozen_string_literal: true

require "active_support" unless defined?(ActiveSupport)

module Lite
  module Uxid
    module Record
      module Nanoid

        extend ActiveSupport::Concern

        included do
          before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) && !uxid? }
        end

        def uxid_prefix
          nil
        end

        private

        def callback_generate_uxid!
          random_nanoid = nil

          loop do
            random_nanoid = Lite::Uxid::Nanoid.encode(prefix: uxid_prefix)
            break unless self.class.exists?(uxid: random_nanoid)
          end

          self.uxid = random_nanoid
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-uxid-1.5.0 lib/lite/uxid/record/nanoid.rb