Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created November 6, 2008 15:39
Show Gist options
  • Save brianjlandau/22618 to your computer and use it in GitHub Desktop.
Save brianjlandau/22618 to your computer and use it in GitHub Desktop.
require 'iconv'
module ActiveSupport
module Inflector
extend self
def slugize(words, slug = '-')
sluged = Iconv.iconv('ascii//TRANSLIT//IGNORE', 'utf-8', words).to_s
sluged.gsub!(/&/, 'and')
sluged.gsub!(/[^\w_\-#{Regexp.escape(slug)}]+/i, slug)
sluged.gsub!(/#{slug}{2,}/i, slug)
sluged.gsub!(/^#{slug}|#{slug}$/i, '')
url_encode(sluged.downcase)
end
def url_encode(word)
URI.escape(word, /[^\w_+-]/i)
end
end
end
module ActiveSupport::CoreExtensions::String::Inflections
def slugize(slug = '-')
ActiveSupport::Inflector.slugize(self, slug)
end
def url_encode
ActiveSupport::Inflector.url_encode(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment