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">

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