Skip to content

Commit

Permalink
- ensure slug is url friendly if user specifies slug
Browse files Browse the repository at this point in the history
- updated README to be more detailed for overall usage
  • Loading branch information
brendan6 committed Oct 13, 2011
1 parent 65b22b2 commit c979440
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
has_unique_slug/*
35 changes: 28 additions & 7 deletions README.md
@@ -1,9 +1,11 @@
# HasUniqueSlug
# Has Unique Slug
Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record

## Install

Add `gem has_unique_slug` to your Gemfile and run `bundle install` to get it installed.
Add `gem 'has_unique_slug'` to your Gemfile and run `bundle install` to get it installed.

Tested and working on Rails 3.1.x

## Usage

Expand All @@ -14,18 +16,22 @@ Assume you have a Post model that has a title and slug column, you can use the f
end


A unique slug will be generated automatically on creation.
If the generated slug is not unique, a number is added onto the end to endure uniqueness. The series starts at 2 and increments
up by one until a unique slug is found.
If a slug is already specified, this slug will be used however the above rules still apply for uniqueness.
A unique slug will be generated automatically on creation by calling parameterize on title.
If the generated slug is not unique, a number is added onto the end to ensure uniqueness. The series starts at 2 and increments up by one until a unique slug is found.
If a slug is already specified, this slug will be used however the above rules still apply for incrementing the slug until a unique one is found.
Ex. Post 1 has title "Sample Post" which would then generate slug "sample-post"
Post 2 has also has title "Sample Post" which then would generate slug "sample-post-2"

You can specify which column to use to generate the slug and which column to use to store the slug. Below is the default:

class Post < ActiveRecord::Base
# the column slug will store the slug, title.parameterize will be called to build the slug
has_unique_slug :slug, :title
end

Or if only 1 argument is given, use that column to store the slug:
The entire argument list is `has_unique_slug(slug_column, title_column, options, &block)` however there are no options you can pass in at this time.

If only 1 argument is given, use that column to store the slug:

class Post < ActiveRecord::Base
has_unique_slug :permalink # Uses the permalink column to store the slug
Expand All @@ -38,6 +44,21 @@ Optionally, a block can be provided to generate the slug:
end
Note the space: parameterize will be called on the result of the block to ensure the slug is url friendly.

You do not have to modify your controller to get this to work:

class PostsController < ApplicationController
# ...
def show
@post = Post.find params[:id]
# you may still use find_by_id to find a record by the database id if need be
end
end

Then you may use all your standard url helpers as normal.
Ex. If a `post` has a title "Sample Post" and a slug "sample-post", the helper `post_path(post)` will create /posts/sample-post

## TODO:

- Would like to write some tests.
Expand Down
2 changes: 1 addition & 1 deletion lib/has_unique_slug.rb
Expand Up @@ -25,7 +25,7 @@ def has_unique_slug(*args, &block)
before_save do |record|

# Add a slug if slug doesn't exist
slug_prefix = record[slug_column].blank? ? build_slug(record, subject_column, &block) : record[slug_column]
slug_prefix = record[slug_column].blank? ? build_slug(record, subject_column, &block) : record[slug_column].parameterize

# Ensure the current slug is unique in the database, if not, make it unqiue
test_slug, i = slug_prefix, 1
Expand Down
2 changes: 1 addition & 1 deletion lib/has_unique_slug/version.rb
@@ -1,3 +1,3 @@
module HasUniqueSlug
VERSION = "0.1.2"
VERSION = "0.1.3"
end

0 comments on commit c979440

Please sign in to comment.