Created
April 28, 2009 01:41
-
-
Save ssoper/102873 to your computer and use it in GitHub Desktop.
SEOize any url
This file contains 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
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