Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Slug generation for ActiveRecord models

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 rakefiles
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 GPL_LICENSE
Octocat-spinner-32 MIT_LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 slugify.gemspec
README.rdoc

slugify

Generate slugs for your models

Install as a rails plugin or or as a gem

…then add the following to a model you'd like to slug:

class MyClass < ActiveRecord::Base
  include Slugify
  slugify :title
end

Throw it in vendor/plugins (with script/plugin install, braid, submodules, or whatever your preferred installation method), and add the following to a model you'd like to slug:

class MyClass < ActiveRecord::Base
  include Slugify
  slugify :title
end

If you are using slugify in multiple models, you may want to mix it into ActiveRecord::Base:

ActiveRecord::Base.instance_eval do
  include Slugify
end

class MyClass < ActiveRecord::Base
  slugify :title
end

Examples

class Page < ActiveRecord::Base
  include Slugify

  validates_presence_of :title

  slugify :title
end

Page.create(:title => "Foo")
# => #<Page id: 1, title: "Foo", slug: "foo">

Page.create(:title => "Foo")
# => #<Page id: 1, title: "Foo", slug: "foo-0">

Using a different slug column

slugify :my_column, :slug_column => :bar

Scoping:

slugify :slug_column, :scope => :col_one
slugify :slug_column, :scope => [:col_one, :col_two]

Conditional slug generation:

slugify :my_column, :when => lambda { |my_record| my_record.published? }

Contributors

kule - STI Issues [Github Issue #1]
Something went wrong with that request. Please try again.