code.dunae.ca

suggests_id Rails plugin

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.

You can browse the source if you like.

Time for an experiment. Updates to plugins, gems and other code will be announced at http://twitter.com/TheCHANGELOG. Follow along so you can receive updates without having to check back. You can also subscribe to the Atom feed.

Installation

./script/plugin install http://code.dunae.ca/svn/suggests_id/trunk

Usage 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

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

Run your input through suggest_id to an ID suggestion.

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

# 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.

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

Contact

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