Module: KSUID::ActiveRecord

Defined in:
lib/ksuid/activerecord.rb,
lib/ksuid/activerecord/type.rb,
lib/ksuid/activerecord/binary_type.rb,
lib/ksuid/activerecord/table_definition.rb

Overview

Enables an Active Record model to have a KSUID attribute

Defined Under Namespace

Modules: TableDefinition Classes: BinaryType, Type

Class Method Summary collapse

Class Method Details

.[](field, created_at: false, binary: false) ⇒ Module

Builds a module to include into the model

Examples:

Add a ‘#ksuid` attribute to a model

class Event < ActiveRecord::Base
  include KSUID::ActiveRecord[:ksuid]
end

Add a ‘#remote_id` attribute to a model and overrides `#created_at` to use the KSUID

class Event < ActiveRecord::Base
  include KSUID::ActiveRecord[:remote_id, created_at: true]
end

Parameters:

  • field (String, Symbol)

    the name of the field to use as a KSUID

  • created_at (Boolean) (defaults to: false)

    whether to override the ‘#created_at` method

  • binary (Boolean) (defaults to: false)

    whether to store the KSUID as a binary or a string

Returns:

  • (Module)

    the module to include into the model



29
30
31
32
33
34
35
36
# File 'lib/ksuid/activerecord.rb', line 29

def self.[](field, created_at: false, binary: false)
  Module
    .new
    .tap do |mod|
      define_attribute(field, mod, binary)
      define_created_at(field, mod) if created_at
    end
end