Skip to content

Instantly share code, notes, and snippets.

@watzon
Created March 19, 2020 00:09
Show Gist options
  • Save watzon/cad6c9c794e53ad2dce9ccec5293badb to your computer and use it in GitHub Desktop.
Save watzon/cad6c9c794e53ad2dce9ccec5293badb to your computer and use it in GitHub Desktop.
EnsureSlug Lucky Mixin
require "cadmium_transliterator"
module EnsureSlug
macro included
before_save generate_slug_if_not_exists
end
def generate_slug_if_not_exists
title.value.try do |title|
if !slug.value || slug.value.try &.empty?
slug.value = Cadmium::Transliterator.parameterize(title)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment