Module: CustomId::Concern

Extended by:
ActiveSupport::Concern
Defined in:
lib/custom_id/concern.rb

Overview

ActiveSupport::Concern that provides the cid class macro for generating prefixed, collision-resistant custom string IDs for ActiveRecord models.

Examples:

Minimal usage – generate a custom primary key

class User < ApplicationRecord
  include CustomId::Concern   # not needed when using the Rails initializer
  cid "usr"
end

User.create!(name: "Alice").id  # => "usr_7xKmN2pQ..."

Embedding shared characters from a related model's ID

class Document < ApplicationRecord
  belongs_to :workspace
  cid "doc", size: 24, related: { workspace: 6 }
end

# If workspace.id == "wsp_ABCDEF...", document.id starts with "doc_ABCDEF..."

Using a non-primary-key column

class Article < ApplicationRecord
  cid "art", name: :slug, size: 12
end

Article.create!(title: "Hello").slug  # => "art_aBcDeFgHiJkL"