The simplest permalink/slug solution for ActiveRecord 3.0
Ruby Perl JavaScript
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles
lib
spec
.gitignore
Appraisals
Gemfile
LICENSE
README.md
Rakefile
parameterize.gemspec

README.md

Parameterize

The simplest permalink/slug solution for ActiveRecord >= 3.0.

It uses ActiveSupport's String#parameterize to create the slug. There are no validations. No slug history. No extra tables or models.

Getting Started

# Gemfile
gem 'parameterize'
$ bundle install
$ rails generate model User name:string param:string
# Parameterize requires a string column named 'param'
# models/user.rb
class User < ActiveRecord::Base
  parameterize :name
end

# elsewhere
user = User.new :name => 'John Doe'
user.valid?
user.param = 'john-doe'
user.to_param = 'john-doe'

Notes

Validations are not included. I prefer to be able to customize my validations directly in the model. The most common validations you would add are:

class Post < ActiveRecord::Base
  parameterize
  validates_presence_of :param
  validates_uniqueness_of :param
end

You could easily use Babosa's #to_identifier or Stringex's #to_url instead of ActiveSupport's #parameterize by just aliasing the method you want to use:

require 'babosa'
class String
  alias :parameterize :to_identifier
end

There is also a Shoulda-style RSpec matcher available for use in your specs:

# spec_helper.rb
require 'parameterize/matcher'

# post_spec.rb
describe Post do
  it { should parameterize(:title) }
end

Copyright

Copyright (c) 2011 Peter Browne. See LICENSE for details.