module Avram::Slugify

Overview

Converts a column value to a URL safe String that can be used as a parameter for finding records. A slug is a String column you define on your model that will be passed through the URL instead of an id.

e.g. /articles/1 -> /articles/how-to-slugify

Use this module in your SaveOperation#before_save.

class Article < BaseModel
  table do
    column title : String
    column slug : String
  end
end

class SaveArticle < Article::SaveOperation
  before_save do
    Avram::Slugify.set slug,
      using: title,
      query: ArticleQuery.new
  end
end

The #set method will use query to determine if that slug has already been used, and fall back to appending a random UUID to the end. If the slug value has already been set, then no new slug will be generated. If you just need the value to set on your own, you can use the #generate method as an escape hatch. This method returns the String value used in the #set method.

class SaveArticle < Article::SaveOperation
  before_save do
    if title.changed?
      slug_value = Avram::Slugify.generate(slug,
        using: title,
        query: ArticleQuery.new)
      slug.value = slug_value
    end
  end
end

Extended Modules

Defined in:

avram/slugify.cr

Instance Method Summary

Instance Method Detail

def generate(slug : Avram::Attribute(String), using slug_candidate : Avram::Attribute(String) | String, query : Avram::Queryable) : String | Nil #

[View source]
def generate(slug : Avram::Attribute(String), using slug_candidates : Array(String | Avram::Attribute(String) | Array(Avram::Attribute(String))), query : Avram::Queryable) : String | Nil #

[View source]
def set(slug : Avram::Attribute(String), using slug_candidate : Avram::Attribute(String) | String, query : Avram::Queryable) : Nil #

[View source]
def set(slug : Avram::Attribute(String), using slug_candidates : Array(String | Avram::Attribute(String) | Array(Avram::Attribute(String))), query : Avram::Queryable) : Nil #

[View source]