fix: update slug size after 2nd attempt

This commit is contained in:
Juan Rodriguez
2024-07-14 21:56:04 +02:00
parent 451a5fbf0f
commit 80cebe3357
2 changed files with 53 additions and 2 deletions
+4 -2
View File
@@ -27,12 +27,14 @@ module App::Controllers::Link
link.url = url
link.user = user
attempts = 0
loop do
slug = Random::Secure.urlsafe_base64(5).gsub(/[^a-zA-Z0-9]/, "")
if !Database.get_by(Link, slug: slug)
slug = Random::Secure.urlsafe_base64(attempts >= 2 ? 6 : 5).gsub(/[^a-zA-Z0-9]/, "")
unless Database.get_by(Link, slug: slug)
link.slug = slug
break
end
attempts += 1
end
changeset = Database.insert(link)