Module: KSUID::ActiveRecord::TableDefinition

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

Overview

Extends ActiveRecord’s table definition language for KSUIDs

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 ActiveRecord::ConnectionAdapters::TableDefinition



26
27
28
# File 'lib/ksuid/activerecord/table_definition.rb', line 26

def ksuid(*args, **options)
  args.each { |name| column(name, :string, **options.merge(limit: 27)) }
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 ActiveRecord::ConnectionAdapters::TableDefinition



49
50
51
# File 'lib/ksuid/activerecord/table_definition.rb', line 49

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