Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Rails plugin that makes assigning a UUID to your models a breeze.
Ruby
Branch: master
Pull request Compare This branch is 6 commits ahead, 3 commits behind norbert:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
LICENSE
README.markdown
Rakefile
init.rb

README.markdown

has_uuid

Provides automated UUID generation for ActiveRecord objects. UUIDs are created after validations on create. It's also possible to do assignment manually, and some other things (see below). Some configuration options are provided if you want to keep your UUIDs in a column other than "uuid" or if you want to use a different UUID generator (default is to generate version 4 UUIDs).

Depends on the uuidtools gem.

Installation

script/plugin install git://github.com/tfe/has_uuid.git

Usage

# normal usage
class Post < ActiveRecord::Base
    has_uuid
end

# don't auto-assign on create
class User < ActiveRecord::Base
    has_uuid :auto => false
end

# store UUID in "token" column, generate version 1 UUIDs
class User < ActiveRecord::Base
    has_uuid :column => :token, :generator => :timestamp
end

Instance Method Usage

# assigns a UUID if a valid one is not already present
@post.assign_uuid

# assigns a UUID, replacing whatever is already there
@post.assign_uuid(:force => true)

# assigns a UUID, replacing whatever was there, and calls save!
@post.assign_uuid!

I'm not sure why you'd use these, but they're there if you want them:

@post.uuid_valid?
@post.uuid_invalid?

If you want to be able to, say, have your application accept requests for resources either by ID or their UUID, then you could do something like this in your WidgetController show method:

Widget.find_by_id_or_uuid(params[:id])

Credit

This was written almost completely by norbert, and I just expanded upon it. joergbattermann also contributed.

Todo

  • It would be cool if the plugin included a migration to add the UUID column. Also: a rake task to run the migration on user-specified table(s). A migration generator would also work and may be better-suited.
  • Provide a rake task for easily assigning UUIDs to existing data.
  • See active_record_uuid for examples of the above.

Contact

Problems, comments, and pull requests all welcome. Find me on GitHub.


Modifications copyright (c) 2009 Todd Eichel for Fooala, Inc., released under the MIT license.

Something went wrong with that request. Please try again.