From c979440b190e4016633165796f603d6d9088db3b Mon Sep 17 00:00:00 2001 From: Brendan Stennett Date: Thu, 13 Oct 2011 18:53:06 -0400 Subject: [PATCH] - ensure slug is url friendly if user specifies slug - updated README to be more detailed for overall usage --- .gitignore | 1 + README.md | 35 +++++++++++++++++++++++++++------- lib/has_unique_slug.rb | 2 +- lib/has_unique_slug/version.rb | 2 +- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 4040c6c..08ba581 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .bundle Gemfile.lock pkg/* +has_unique_slug/* diff --git a/README.md b/README.md index 205b790..48d7e31 100644 --- a/README.md +++ b/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 @@ -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 @@ -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. diff --git a/lib/has_unique_slug.rb b/lib/has_unique_slug.rb index 8a94d37..b5c3589 100644 --- a/lib/has_unique_slug.rb +++ b/lib/has_unique_slug.rb @@ -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 diff --git a/lib/has_unique_slug/version.rb b/lib/has_unique_slug/version.rb index 8a9fad3..a7cf904 100644 --- a/lib/has_unique_slug/version.rb +++ b/lib/has_unique_slug/version.rb @@ -1,3 +1,3 @@ module HasUniqueSlug - VERSION = "0.1.2" + VERSION = "0.1.3" end