Module: ActiveRecord::KSUID::TableDefinition

Defined in:
lib/active_record/ksuid/table_definition.rb

Overview

Extends ActiveRecord’s table definition language for KSUIDs

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#ksuid(*args, **options) ⇒ void

This method returns an undefined value.

Defines a field as a string-based KSUID

Examples:

Define a KSUID field as a non-primary key

ActiveRecord::Schema.define do
  create_table :events, force: true do |table|
    table.ksuid :ksuid, index: true, unique: true
  end
end

Define a KSUID field as a primary key

ActiveRecord::Schema.define do
  create_table :events, force: true, id: false do |table|
    table.ksuid :id, primary_key: true
  end
end

Parameters:

  • args (Array<Symbol>)

    the list of fields to define as KSUIDs

  • options (Hash)

    see ConnectionAdapters::TableDefinition

Options Hash (**options):

  • :prefix (String)

    the prefix expected in front of the KSUID

Since:

  • 0.5.0



27
28
29
30
31
# File 'lib/active_record/ksuid/table_definition.rb', line 27

def ksuid(*args, **options)
  prefix_length = options.delete(:prefix)&.length || 0

  args.each { |name| column(name, :string, **options.merge(limit: 27 + prefix_length)) }
end

#ksuid_binary(*args, **options) ⇒ void

This method returns an undefined value.

Defines a field as a binary-based KSUID

Examples:

Define a KSUID field as a non-primary key

ActiveRecord::Schema.define do
  create_table :events, force: true do |table|
    table.ksuid_binary :ksuid, index: true, unique: true
  end
end

Define a KSUID field as a primary key

ActiveRecord::Schema.define do
  create_table :events, force: true, id: false do |table|
    table.ksuid_binary :id, primary_key: true
  end
end

Parameters:

  • args (Array<Symbol>)

    the list of fields to define as KSUIDs

  • options (Hash)

    see ConnectionAdapters::TableDefinition

Since:

  • 0.5.0



52
53
54
# File 'lib/active_record/ksuid/table_definition.rb', line 52

def ksuid_binary(*args, **options)
  args.each { |name| column(name, :binary, **options.merge(limit: 20)) }
end