0.0
Low commit activity in last 3 years
No release in over a year
Use `has_token` in your model to automatically generate a unique token on before_create. (Model needs to have a `token` field with type string.)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.0
 Project Readme

da-has_token

A token generator for Ruby on Rails 3 and Active Record.

Installation

Put the following gem requirement in your Gemfile:

gem "da-has_token"

Usage

Make sure your model has a field called token (type string).

class Entry < ActiveRecord::Base  
  # Use default options (size 10, chars a-z + A-Z + 0-9)
  has_token
  
  # Use custom token size
  # has_token size: 25
  
  # Use custom set of characters
  # has_token chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end

The token is set in a before_create callback. There is a possible race condition when checking if a newly generated token already exists – make sure to add an unique index to the token column in the database.

A NoMoreTokens error is raised if a new unique token can not be generated within HasToken::TokenGenerator::MAX_TRIES.

There are similar plugins providing more features configuration options, e.g. https://github.com/mindbox/has_token.

This project is released under the MIT license.