| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Slug provides simple, straightforward slugging for your ActiveRecord models.
Slug is based on code from Norman Clarke's fantastic friendly_id project and Nick Zadrozny's friendly_identifier.
What's different:
Add the gem to your Gemfile
gem 'slug'
of your rails project.
This is tested with Rails 5.1.4, MRI Ruby 2.4.1
It's up to you to set up the appropriate column in your model. By default, slug saves the slug to a column called 'slug', so in most cases you'll just want to add
add_column :my_table, :slug, :stringin a migration. You should also add a unque index on the slug field in your migration
add_index :model_name, :slug, unique: trueThough Slug uses validates_uniqueness_of to ensue the uniqueness of your slug, two concurrent INSERTs could try to set the same slug.
Once your table is migrated, just add
slug :source_fieldto your ActiveRecord model. :source_field is the column you'd like to base the slug on. For example, it might be :headline.
The source column isn't limited to just database attributes—you can specify any instance method. This is handy if you need special formatting on your source column before it's slugged, or if you want to base the slug on several attributes.
For example:
class Article < ActiveRecord::Base
slug :title_for_slug
def title_for_slug
"#{headline}-#{publication_date.year}-#{publication_date.month}"
end
endwould use headline-pub year-pub month as the slug source.
From here, you can work with your slug as if it were a normal column. find_by_slug and named scopes will work as they do for any other column.
There are two options:
If you want to save the slug in a database column that isn't called slug, just pass the :column option. For example:
slug :headline, column: :web_slug
would generate a slug based on headline and save it to web_slug.
If the source column is empty, blank, or only contains filtered characters, you can avoid ActiveRecord::ValidationError exceptions by setting generic_default: true. For example:
slug :headline, generic_default: truewill generate a slug based on your model name if the headline is blank.
This is useful if the source column (e.g. headline) is based on user-generated input or can be blank (nil or empty).
Some prefer to get the exception in this case. Others want to get a good slug and move on.
Ben Koski, ben.koski@gmail.com
With generous contributions from:
| Back | FazBrowse Home | New Git URL |