Skip to content

Commit

Permalink
Create unique random slug if permalinks are created without
Browse files Browse the repository at this point in the history
  • Loading branch information
janwerkhoven committed Apr 7, 2024
1 parent ccd2fd2 commit 8f54381
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion app/controllers/v1/admin/permalinks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def show
end

def create
allow_create
allow_create(
{
slug: unique_slug
}
)
end

def update
Expand Down Expand Up @@ -39,11 +43,35 @@ def creatable_attributes
]
end

def creatable_relationships
%i[
event
]
end

def permitted_filters
%i[
slug
]
end

def unique_slug
slug = nil
unique = false

until slug.present? && unique
slug = three_random_letters
record = Permalink.find_by slug: slug
unique = record.nil?
end

slug
end

def three_random_letters
charset = Array('A'..'Z') + Array('a'..'z')
Array.new(3) { charset.sample }.join
end
end
end
end

0 comments on commit 8f54381

Please sign in to comment.