SpeakingId
Lightweight Ruby on Rails gem to handle slugs for Active Record objects.
Installation
Rails 3
Add the gem to your project’s Gemfile as follows:
gem 'speaking_id'
Then install the gem by running:
bundle install
Alternatively you can install it as a Rails plugin.
rails plugin install git://github.com/martinjagusch/speaking_id.git
Rails 2
In Rails 2 you can set it up in your environment.rb file.
config.gem "speaking_id"
And then install the gem.
sudo rake gems:install
Or as a Rails plugin.
script/plugin install git://github.com/martinjagusch/speaking_id.git
Instructions
In your models:
class User < ActiveRecord::Base
has_slug :name
end
class Project < ActiveRecord::Base
has_random_slug
end
In your migrations:
class AddSpeakingIdToUserAndProject < ActiveRecord::Migration
def self.up
add_column :users, :slug, :string
add_column :project, :slug, :string
add_index :users, :slug, :unique => true
add_index :project, :slug, :unique => true
end
def self.down
remove_column :users, :slug
remove_column :project, :slug
end
end
By default, SpeakingId uses a column called “slug”. If you want to use a different column name, just pass the :column
option:
has_slug :title, :column => :column_name
This works also with the has_random_slug
method.
The gem also adds an instance method to the String class called normalize
. The method replaces known special characters and removes unknown characters in a string.