Skip to content

Commit 8f54381

Browse files
committed
Create unique random slug if permalinks are created without
1 parent ccd2fd2 commit 8f54381

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

app/controllers/v1/admin/permalinks_controller.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def show
1010
end
1111

1212
def create
13-
allow_create
13+
allow_create(
14+
{
15+
slug: unique_slug
16+
}
17+
)
1418
end
1519

1620
def update
@@ -39,11 +43,35 @@ def creatable_attributes
3943
]
4044
end
4145

46+
def creatable_relationships
47+
%i[
48+
event
49+
]
50+
end
51+
4252
def permitted_filters
4353
%i[
4454
slug
4555
]
4656
end
57+
58+
def unique_slug
59+
slug = nil
60+
unique = false
61+
62+
until slug.present? && unique
63+
slug = three_random_letters
64+
record = Permalink.find_by slug: slug
65+
unique = record.nil?
66+
end
67+
68+
slug
69+
end
70+
71+
def three_random_letters
72+
charset = Array('A'..'Z') + Array('a'..'z')
73+
Array.new(3) { charset.sample }.join
74+
end
4775
end
4876
end
4977
end

0 commit comments

Comments
 (0)