Project

has_uuid

0.01
No commit activity in last 3 years
No release in over 3 years
The has_uuid gem adds a UUID to your AR models. See the README for details.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

 Project Readme

has_uuid

has_uuid provides basic UUID assignment methods for ActiveRecord objects.

It depends on the uuidtools gem.

Initial credits go to has_uuid.

Travis build status: Travis build status

Installation

Add

gem 'has_uuid'

to your Gemfile and run

bundle install

Finally, you need to create and run a migration which adds an uuid column to the model to which you want to add an UUID:

class AddUuidToOrders < ActiveRecord::Migration
  def change
    add_column :orders, :uuid, :string
  end
end

Usage

class Post < ActiveRecord::Base
  # automatically assign a UUID to the "uuid" column on create
  has_uuid
end

class Comment < ActiveRecord::Base
  # skip assignment on create
  has_uuid :auto => false
end

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

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

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

# assign a UUID and save, replacing whatever is there
@post.assign_uuid!

# check if the current UUID is valid
@post.uuid_valid?

# Generate a UUID to use it later
Post.generate_uuid