0.0
No commit activity in last 3 years
No release in over 3 years
HasSecureUuid provides you an easily way to generate random uuids for any model in ruby on rails. **SecureRandom::uuid** is used to generate a unique uuid, so collisions are highly unlikely.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0

Runtime

 Project Readme

HasSecureUuid

HasSecureUuid provides an easy way to generate uniques random uuid's for any model in ruby on rails. SecureRandom::uuid is used to generate the unique id's, so collisions are highly unlikely.

Note If you're worried about possible collissions, there's a way to generate a race condition in the database in the same way that validates_uniqueness_of can. You're encouraged to add an unique index in the database to deal with this even more unlikely scenario.

Installation

Add this line to your application's Gemfile:

gem 'has_secure_uuid'

And then run:

$ bundle

Or install it yourself as:

$ gem install has_secure_uuid

Setting your Model

The first step is to generate a migration in order to add the uuid key field.

rails g migration AddidentifierToUsers uuid:string
=>
   invoke  active_record
   create    db/migrate/20150424010931_add_uuid_to_users.rb

Then run rake db:migrate in order to update users table in the database. The next step is to add has_secure_uuid to the model:

# Schema: User(uuid:string, identifier:string)
class User < ActiveRecord::Base
  has_secure_uuid
end

user = User.new
user.save
user.uuid # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
user.regenerate_uuid # => true

To use a custom column to store the uuid field you can specify the column_name option. See example above (e.g: identifier):

# Schema: User(uuid:string, identifier:string)
class User < ActiveRecord::Base
  has_secure_uuid :identifier
end

user = User.new
user.save
user.identifier # => "6c3d256c-aaa7-443a-a16b-75a99ecde277"
user.regenerate_identifier # => true

Running tests

Running

$ rake test

Should return

3 runs, 5 assertions, 0 failures, 0 errors, 0 skips

Contributing

  1. Fork it ( https://github.com/sndrgrdn/has_secure_uuid/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Modified from: https://github.com/robertomiranda/has_secure_token