Skip to content

Instantly share code, notes, and snippets.

@nuwansh
Created March 20, 2012 12:52
Show Gist options
  • Save nuwansh/2135014 to your computer and use it in GitHub Desktop.
Save nuwansh/2135014 to your computer and use it in GitHub Desktop.
Takes the slug, downcases it and replaces non-word characters with a -
# Takes the slug, downcases it and replaces non-word characters with a -.
# Feel free to override this method if you'd like different slug formatting.
def normalize_slug_url
return if self.name.blank?
s = ActiveSupport::Multibyte.proxy_class.new(self.name).normalize(:kc)
s.downcase!
s.strip!
s.gsub!(/[^a-z0-9\s-]/, '') # Remove non-word characters
s.gsub!(/\s+/, '-') # Convert whitespaces to dashes
s.gsub!(/-\z/, '') # Remove trailing dashes
s.gsub!(/-+/, '-') # get rid of double-dashes
self.url = s.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment