UniqIdentifier

Code Climate

Dependency Status

Build Status (Travis CI)

Coverage Status

Gem Version

Add an uniq identifier on your models. For make your model agnostic from backend unique identifier.

Installation

Add this line to your application's Gemfile:

gem 'uniq_identifier'

And then execute:

$ bundle

Or install it yourself as:

$ gem install uniq_identifier

Usage

add uuid in your model like that:

class Foo < ActiveRecord::Base
  uniq_identifier
end
foo = Foo.new
foo.id   # => nil
foo.uuid # => "0c6bbc03-a269-44e2-8075-f442e1aac0c8"
foo.create!
foo.id # => 1
foo.uuid # => "0c6bbc03-a269-44e2-8075-f442e1aac0c8"
my_foo = Foo.where(uuid: "0c6bbc03-a269-44e2-8075-f442e1aac0c8")
my_foo.id # => 1

Configuration

add app/config/initializers/uniq_identifier.rb

UniqIdentifier.configuration do |conf|
  conf.generator = SecureRandom
end

you can use the generator

rails g uniq_identifier:install
rails g uniq_identifier:add <model>

for mongoid use

rails g uniq_identifier:add <model> --orm=mongoid

Options

class CustomGenerator
  def uuid
    # ...
  end
end

class Bar
  uniq_identifier generator: CustomGenerator,
                  auto: bool,
                  validate: bool
end
  • generator can be any object which respond to uuid signal or :default (default: :default)
  • auto if set to false then uuid will not be automatically generated after initialize (default: true)
  • validate if set to false validation will not be added (default: true)

Contributing

  1. Fork it ( https://github.com/[my-github-username]/uniq_identifier/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

Test

  bundle && ADAPTER=mongoid bundle
  rake