Simple make_permalink Gem

make_permalink is a simple gem that allows you to easily create permalinks (url-friendly-output) for methods in your objects. You don’t need any database storage to make it work which is just awesome!

Example


  class Post
    make_permalink :title
  end

  p = Post.create(:title => "Hello World!")
  p.permalink   # => "1-hello-world"

If you’re using rails and you want your url’s to have the permalink and not only the id, just add the to_param method to your model


  class Post < ActiveRecord::Base
    make_permalink :title

    def to_param
      permalink
    end
  end

Install

It’s really simple to install this magnificent gem. Just run a small gem install make_permalink and you’ve got it!

Well, not quite, you may have the gem installed in your system, but when you share the beautiful code you’ve created, people won’t actually know that. So, that’s when Bundler appears.

Add gem 'make_permalink' to your Gemfile and then run bundle install. Well, and remember to check your Gemfile in your repo.

Options

Non ascii characters

You can have a permalink that won’t change non-ascii.

:replace_non_ascii : If set to false non-ascii chars won’t be replaced. Default is true


  class Post
    make_permalink :title, :replace_non_ascii => false
  end

  p = Post.create(:title => "Let's rock & roll")
  p.permalink   # => "1-let-s-rock-roll"

Notice that if you don’t replace non-ascii chars the permalik would be way cooler (thanks to the stringex gem)


  # You will get this with the default behavior 
  p.permalink  # => "1-lets-rock-and-roll"

Remove id from permalink

I don’t really like to have the ID of the object on the permalink, so I just added an option to remove it. Just call the method with :include_id => false and you’re done!

:include_id is true by default but just change it to remove the id from the permalink.

Internally the gem will look for the create_permalink_prefix(boolean) method so if you want to change the prefix, just override that method and you’ll be good to go in no time!

Bugs/Contact

If you need to contact me about a bug or anythig, please open an issue here

Also, feel free to patch it as needed, every fix/refactoring/extension is more than welcome!

Contributors

Nicolás Hock (nhocki) Federico Builes (febuiles) Sven Fuchs (svenfuchs) for the Stringex gem

Copyright © 2011 Nicolás Hock Isaza, released under the MIT license