Low commit activity in last 3 years
No release in over a year
Auto-generate unique identifier value for Active Record
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 7.0.1

Runtime

~> 0.2.0
 Project Readme

ActsAsIdentifier

Gem Version

Automatically generate unique fixed-length string for one or more columns of ActiveRecord based on sequence column

Usage

ActsAsIdentifier only generate identifier before_commit

class Account < ActiveRecord::Base
  include ActsAsIdentifier
  #
  # == default options
  #
  #      attr: :identifier,
  #      seed: 1,
  #    length: 6,
  #    prefix: nil,
  # id_column: :id,
  #     chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  #
  acts_as_identifier
  # or customize options:
  acts_as_identifier :slug, length: 6, prefix: 's-'
end
# => [Account(id: integer, name: string, tenant_id: string, slug: string)]

Account.create
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, slug: "s-EPaPaP">
Account.create
# => #<Account:0x00007fcdb90830c0 id: 2, name: nil, tenant_id: nil, slug: "s-HSo0u4">

Features

  • ActiveRecord.find_by_decoded_identifier('s-EPaPaP') => Actually find by id, id is decoded from identifier, with this method you don't need to add extra index for identifier
  • ActiveRecord.find_by_decoded_identifier!('s-EPaPaP') => equal to find_by_xx!
  • ActiveRecord.decode_identifier('s-EPaPaP') => decode identifier to id
  • ActiveRecord.encode_identifier(1) => encode id to identifier
  • ActiveRecord.identifier_encoder => encoder instance, see xencoder

Installation

bundle add acts_as_identifier

Requirements

Use gem Xencoder to encode sequence number to fixed-length string.

Contributing

Contribution directions go here.

Testing

bundle exec rspec