[edge Rails] Inflector.parameterize for easy slug generation

David has just commited an parameterize method for easy slug generation. This means it strips out all special characters so that it can be savely used in URLs. It replaces anything but a-z and 0-9 with a “-” (you can pass a custom seperator).
Example to generate nice URLs for your models:

def to_param
  "#{id}-#{name.parameterize}"
end
"$%hello I'm a sentence with & a löt òf SPECIAL chars+".parameterize #=> -hello-i-m-a-sentence-with-a-lt-f-special-chars-

My find_by_param plugin, that helps you a lot with working with nice URLs, uses a custom encoding method to do this. Perhaps I will change that some time soon. ;)

Update:
The comments on the commit pointed to two really nice projects:
1. stringex – which is a bit of a overkill. *tries to solve everything* but creates really aweseome slugs by translating special chars ($ to dollar, etc.)
2. slugalizer – a ruby slugalizer which ses ActiveSupport for platform-consistent normalization. It also does nice formatting like: Åh, räksmörgåsar! => ah-raksmorgasar”

Update2
This feature was extended earlier today. Not nice conversions like Malmö = malmo or Garçons = garcons are supported.

Comments

5 Responses to “[edge Rails] Inflector.parameterize for easy slug generation”

  1. tobstarr on September 10th, 2008 1:29 pm

    Because of the lack of support for umlauts the implementation is way to simple for being usable for the projects I work on.

  2. Bumi on September 10th, 2008 2:06 pm

    Yes, that’s true. This is a very basic and save implementation. Though I’m also removing umlauts and any special char from the slug. ;)
    What do you allow in your URLs? which regex do you use?

  3. tobstarr on September 10th, 2008 3:51 pm

    Some gsub replacements using a hash like that before generating the slug:
    repl = {
    ‘ä’ => ‘ae’, ‘ö’ => ‘oe’, ‘ü’ => ‘ue’, ‘ß’ => ‘ss’,
    ‘Ä’ => ‘Ae’, ‘Ö’ => ‘Oe’, ‘Ü’ => ‘Ue’,
    ‘á’ => ‘a’, ‘ã’ => ‘a’, ‘é’ => ‘e’, ‘í’ => ‘i’, ‘ó’ => ‘o’, ‘ú’ => ‘u’,
    ‘ñ’ => ‘n’,
    ‘Á’ => ‘A’, ‘É’ => ‘E’, ‘Í’ => ‘I’, ‘ø’ => ‘o’, ‘Ø’ => ‘O’, ‘Ó’ => ‘O’, ‘Ú’ => ‘U’,
    ‘å’ => ‘a’, ‘à’ => ‘a’, ‘è’ => ‘e’, ‘ì’ => ‘i’, ‘ò’ => ‘o’, ‘ù’ => ‘u’,
    ‘À’ => ‘A’, ‘È’ => ‘E’, ‘Ì’ => ‘I’, ‘Ò’ => ‘O’, ‘Ù’ => ‘U’,
    ‘â’ => ‘a’, ‘ê’ => ‘e’, ‘î’ => ‘i’, ‘ô’ => ‘o’, ‘û’ => ‘u’,
    ‘Â’ => ‘A’, ‘Å’ => ‘A’, ‘Ê’ => ‘E’, ‘Î’ => ‘I’, ‘Ô’ => ‘O’, ‘Û’ => ‘U’
    }

  4. Bumi on September 10th, 2008 5:40 pm

    ah, ok… that’s really nice.
    The comments on the commit message also pointed to two really nice projects to do something similiar.

  5. Bumi on September 11th, 2008 5:12 pm

    You should follow the discussion on the commit:
    http://github.com/rails/rails/commit/b8e8be83e952163e225f9b38bd7251cba9c44f38#comments

    A patch that improves the implementation is proposed.