Skip to content

Instantly share code, notes, and snippets.

@ssoper
Created April 28, 2009 01:41
Show Gist options
  • Save ssoper/102873 to your computer and use it in GitHub Desktop.
Save ssoper/102873 to your computer and use it in GitHub Desktop.
SEOize any url
def slug
slug = title.downcase.gsub(/'s/, '').gsub(/[^[:alnum:]\-\s\_]/, '').split(/[\s\-\_]+/).delete_if { |i| i.empty? }
limit_reached = false
# This ensures only whole words are used
slug = slug.inject('') do |str, token|
new_token = str.length == 0 ? token : "-#{token}"
limit_reached = true if (str.length + new_token.length) > 50
limit_reached ? str : str << new_token
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment