Generate unique login names, text IDs and URLs with this Ruby on Rails plugin.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
CHANGELOG
MIT-LICENSE
README.rdoc
TODO
init.rb
rakefile

README.rdoc

Suggests ID Rails Plugin README

Suggests an identifier/url chunk based on provided fields.

I use this plugin to generate login names, e-mail addresses and URLs for user-entered content.

The plugin takes one or more strings as input, sanitizes the input and generates a list of possible IDs. The shortest ID is returned.

Installation

./script/plugin install git://github.com/alexdunae/suggests_id.git

Examples

In your model, specify which column holds the identifier - this is used when checking for duplicates.

class User < ActiveRecord::Base
  suggests_id :target => :login_name
end

>> User.suggest_id('John Smith')
=> "johnsmith"

Suffixes

You can also add a suffix. The following code will check for IDs between title-0000 and title-0049 if johnsmith is not available.

class User < ActiveRecord::Base
  suggests_id :target => :login_name
              :suffix => '-0000', 
              :suffix_iterations => 50
end

# 'title' is available
>> User.suggest_id('Title')
=> "title"

# 'title' already taken
>> User.suggest_id('Title')
=> "title-0000"

# 'title-0000' already taken
>> User.suggest_id('Title')
=> "title-0001"

Disposable words

Optionally, you can add an array of disposable words which will be trimmed out when generating IDs.

class User < ActiveRecord::Base
  suggests_id :target => :login_name, :disposable => ['mr', 'mrs', 'ms', 'miss']
end

# Disposable words are removed if possible
>> User.suggest_id(['Mr.', 'John', 'Smith'])
=> "johnsmith"

Testing

The unit tests for this plugin use an in-memory sqlite3 database (www.sqlite.org/).

To execute the unit tests run the default rake task (rake). To execute the unit tests but preserve to debug log run rake test.

Credits and code

Written by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2007-09.

The project page is code.dunae/suggests_id/.

Thanks to David Baldwin (www.baldwindigital.net/) for suggestions for version 1.1.