tokenize
tokenize
generates unique strings for ActiveRecord models before creation. This works well for setting API keys, temporary passwords, etc. tokenize
is also great for creating forward-facing IDs for models such as products or orders where you may not want to use the numeric primary key and reveal your volume.
Installation
script/plugin install git://github.com/laserlemon/tokenize.git
Example
In your migration:
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :token, :limit => 8
t.
end
In your model:
class User < ActiveRecord::Base
validates_presence_of :first_name, :last_name
tokenize
end
In your controller:
user = User.create(:first_name => 'Steve', :last_name => 'Richert')
Tips
-
You can pass multiple token columns into the
tokenize
or omit the column names to use the default (:token
). -
You can pass
:length
and:characters
options to thetokenize
method for finer control over the token format. The default is an 8-character string of lowercase and uppercase letters as well as numbers, producing 218,340,105,584,896 possible tokens.