UniqIdentifier
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 touuid
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
- Fork it ( https://github.com/[my-github-username]/uniq_identifier/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Test
bundle && ADAPTER=mongoid bundle
rake