Project

token_attr

0.0
No commit activity in last 3 years
No release in over 3 years
Unique random token generator for ActiveRecord
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.5
>= 0
>= 0
>= 0

Runtime

>= 4.0.0
 Project Readme

token_attr

Unique random token generator for ActiveRecord.

Installation

Add token_attr to your Gemfile:

gem 'token_attr', '~> 0.2.0'

Usage

class User < ActiveRecord::Base
  include TokenAttr::Concern
  token_attr :token
end

user = User.new
user.valid?
user.token # => "b8bd30ff"

The token is generated in a before_validation callback only if the it's nil.

Options

Length

The length of the token to generate.

Default: 8

token_attr :token, length: 40

Alphabet

The alphabet to use to generate the token.

Uses hexadecimal characters by default.

Accepted values:

  • :alphabetic - any character from a to z (both lower and upper case)
  • :numeric - any number
  • :alphanumeric - any character or number
  • a string - a string of your choice of the characters you want to use
token_attr :token, alphabet: :numeric      # => "82051173"
token_attr :token, alphabet: :alphabetic   # => "xqnInSJa"
token_attr :token, alphabet: :alphanumeric # => "61nD0lUo"
token_attr :token, alphabet: "token"       # => "ktnekoet"

Contributing

  1. Fork it ( http://github.com/mbillard/token_attr/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 new Pull Request