Created
August 21, 2022 14:58
-
-
Save Merovex/9e9c626400c52e0f47d402f631268d81 to your computer and use it in GitHub Desktop.
Create random slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Slug | |
extend ActiveSupport::Concern | |
def self.included(base) | |
base.extend ClassMethods | |
base.before_create :set_slug | |
end | |
def to_param | |
[title.to_s.parameterize, slug].join('-') | |
end | |
def set_slug | |
loop do | |
# self.slug = SecureRandom.base64(4).tr('+/=', '') | |
self.slug = SecureRandom.uuid.split('-').first | |
break unless self.class.find(slug) | |
end | |
end | |
module ClassMethods | |
# https://github.com/hungrymedia/superslug | |
# Overloading the default find_by method to allow for slug or id | |
def find_by!(arg, *args) | |
unless arg[:id].nil? | |
input = arg[:id] | |
if input.to_i.to_s != input.to_s | |
arg[:slug] = input.split('-').last | |
arg.delete(:id) # remove the ID from the hash since it's not a valid attribute | |
end | |
end | |
super | |
end | |
# https://github.com/hungrymedia/superslug | |
# Overloading the default find method to allow for slug or id | |
def find(input) | |
if input.instance_of?(Array) | |
super | |
else | |
input.to_i.to_s == input.to_s ? super : find_using_slug(input) | |
end | |
end | |
def find_using_slug(param) | |
slug = param.split('-').last || param | |
where(slug:).limit(1).first | |
end | |
# create random/unique slug | |
def unique_slug(key) | |
loop do | |
# slug = SecureRandom.base64(4).tr('+/=', '') | |
slug = SecureRandom.uuid.split('-').first | |
return slug unless where({ key.to_sym => slug }).exists? | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment