Create the uid(unique identifier) attribute in your model or class.
Add this line to your application's Gemfile:
gem 'uidable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install uidable
- Create a migration to add the uid column in your table.
- Include uidable in your model.
- Setup uidable with/without options in your model.
- Uid is generated and ready to use when a record is created. Note that the uid is still nil when the record is initialized but haven't be saved.
A migration example:
class AddUidToMyModels < ActiveRecord::Migration
def change
add_column :my_models, :uid, :string, null: false
add_index :my_models, :uid, unique: true
end
end
A model example:
class MyModel
include Uidable
uidable
end
a = MyClass.new
a.uid # nil
a.save
a.uid "cmerft8rotdy7wvmtxc63ljoxos67bc8"
- Include uidable in your class.
- Setup uidable with/without options in your class.
- Uid is generated and ready to use when an instance is initialized.
A class example:
class MyClass
include Uidable
uidable uid_size: 64, read_only: false
end
a = MyClass.new
a.uid # "zcf45ltmkyh4w2ofsc1rp8dka6wi4flt3h3szwo1z4rkfsvk387mclg1cikutbc7"
Please reference tests for more usage examples.
The default uid attribute is named with "uid", you can change it with uid_name: <name>
. Note that you need change the column name in your migration as well.
The default uid is a 32-bit length string with numbers and alphabets. You can change the uid size with uid_size: <size>
. If you want to generate the uid with your own way, please see [Redefine uid generation].
The uid is read only by default. You can disabled it with read_only: false
.
The presence validation is enabled by default. You can disable them with presence: false
.
There are three options for the uniqueness validation - :create
, :always
, :none
. :create
is the default option, it means only check the uniqueness when creating a record. :always
means doing uniqueness validation each time when the model is saved. :none
means the uniqueness validation is disabled. Note that you should change your migration to support uniqueness as well if needed.
If the option set_to_param: true
is given, the to_param
is overridden with uid and it means you can use uid in your routes path.
If the option scope: true
is given, a scope with_uid
is created and you can use it to find records with uid. Note that if you change the uid name with the option uid_name: <name>
, the scope is also changed to with_<name>
.
You can override gen_<name>
method in your class/model if you want to generate your own uid. Here is an example:
require `random_token`
class MyModel < ActiveRecord::Base
include Uidable
uidable
private
def gen_uid
RandomToken.gen(64, s: 8)
end
end
Run ruby test/test_all.rb
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
- Fork it ( https://github.com/sibevin/uidable/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
Sibevin Wang
Copyright (c) 2015 Sibevin Wang. Released under the MIT license.