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.
script/plugin install git://github.com/tfe/has_uuid.git
# 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
# 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])
This was written almost completely by norbert, and I just expanded upon it. joergbattermann also contributed.
- 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.
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.