mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-12 23:09:03 +01:00
22 lines
393 B
Ruby
22 lines
393 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
# Generate a unique token
|
||
|
class GenerateTokenService
|
||
|
def call(model_class = Order)
|
||
|
loop do
|
||
|
token = "#{random_token}#{unique_ending}"
|
||
|
break token unless model_class.exists?(token: token)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def random_token
|
||
|
SecureRandom.urlsafe_base64(nil, false)
|
||
|
end
|
||
|
|
||
|
def unique_ending
|
||
|
(Time.now.to_f * 1000).to_i
|
||
|
end
|
||
|
end
|