Slugable

Installation

Add this line to your application's Gemfile:

gem 'slugable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install slugable

Usage

in model use method has_slug

class Item < ActiveRecord::Base
  attr_accessor :name, :slug

  has_slug # default :from => :name, :to => :slug
end

# then in code
item = Item.create!(:name => "my name is")
item.slug # => "my-name-is"

item.to_slug # => "my-name-is"

you can override defaults by passing hash

class Page < ActiveRecord::Base
  attr_accessor :title, :seo_url

  has_slug :from => :title, :to => :seo_url
end

# then in code
page = Page.create!(:title => "my name is", :seo_url => "my url")
page.seo_url # => "my-url"
page.to_seo_url # => "my-url"

if you have model with ancestry gem 'https://github.com/stefankroes/ancestry'

class Category < ActiveRecord::Base
  attr_accessor :name, :slug

  has_ancestry
  has_slug
end

# then in code
root = Category.create!(:name => "root", :slug => "root")
root.slug # => "root"
root.to_slug # => ["root"]

child = Category.new(:name => "child", :slug => "child")
child.parent = root
child.save!

child.slug # => "child"
child.to_slug # => ["root", "child"]

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request