Slugger

SVN Repository

http://svn.jesuscarrera.info/slugger

README

With Slugger you can use a field(s) of the model as a slug (URL friendly version of the record). So instead of controller/id you can use controller/slug (http://example.com/posts/1 => http://example.com/posts/my-post-title)

Formatting examle: “- This is The title_of_my_pOst with weird f@rm-matting aND áccénts /— “ will be transformed in “this-is-the-title-of-my-post-with-weird-f-rm-matting-and-accents”

You need an extra field in that model to store the slug (’slug’ by default).
You can use the provided migration 00#_add_field_to_model.rb.

You can use Slugger.escape to get slugs within your app. Example: Slugger.escape(’My text’) #=> ‘my-text’

By default if the slug is taken, a -# is added at the end of the new slug: post-title, post-title-2, post-title-3, etc.

You can use one field as scope in which the slug must be different. For example one post title in one blog can be the same as one post title in other blog.

By default if the field is updated, the slug remains the same.
That is because the URLs shoud be permanently the same despite the updates, or make a 301 redirection.
You can change this behaviour to change the slug anyway, or to make an automatic 301 redirection from the old slug to the new.
If you want to use the redirection option you need an extra model to store the redirection.
Use the migration 00#_create_slug_redirections.rb and add the model slug_redirection.rb

You don’t need change anything in your controllers. You can use Model.find(params[:id]) like always, but now params[:id] is the slug.
You can still access to the records of this model by ID, but only from the controller (not from the URL).

If the record or the redirection wasn’t found raises an ActiveRecord::RecordNotFound, that you can rescue normally from your controller.

Installation

script/plugin install http://svn.jesuscarrera.info/slugger

Examples

Use case

We need use slugs for the title of our blog posts (field ‘title’). We have a field ‘permalink’ to store the slug in.
We have different blogs, so we can have the same slug for each blog.
On updates if the title change, we want to change the slug as well, but doing a 301 redirection from the old slug to the new.

Notes

This plugin was inspired mainly in permalink_fu and sluggable_finder
Thanks!!

Some part of the code, mainly for the redirection stuff, is a bit hacky, so maybe you can suggest another solution. See code comments.

To-do

- Tests
- Install/rakes
- Clean hacky code???

License

Copyright (c) 2008 Jesus Carrera (), released under the MIT license

3 Comments

  • [...] En las últimas semanas he ocupado algo de mi tiempo libre en desarrollar mi primer plugin para Rails. Teniendo en cuenta que no soy ningún experto en metaprogramming, en general estoy bastante contento con el resultado: el plugin hace exactamente lo que quería y yo he aprendido cosas. Básicamente lo que hace es crear URL amigables para acceder a nuestros registros de la base de datos. Entrar en su página para obtener más información (en inglés). Lo he publicado en algunos sitios.  [...]

    | Sunday, 13 April 2008
  • Nice!

    | Monday, 14 April 2008
  • Thanks to Julien Ramel I’ve corrected 2 issues:
    1. Add the migrations to the repository.
    2. Correct bug in the escape function.

    | Monday, 19 May 2008

Leave a comment