Module: OpenLoam::GeneratedKey

Included in:
AuthAttempt, Config, MfaCredential, Tenant, TenantRecord, User
Defined in:
lib/open_loam/generated_key.rb

Overview

Primary-key generation for tables whose key is not an integer.

Rails leaves key generation to the database, which works for integer keys (a sequence or AUTOINCREMENT) and for uuid columns on Postgres, where create_table installs a gen_random_uuid() default. A string primary key has neither: the INSERT sends NULL and the row is rejected or lands with an empty key.

OpenLoam's tables follow the host app's key type (see OpenLoam::Generators::PrimaryKeyOptions), so on a string-keyed app every OpenLoam model needs its key generated in Ruby. Before this, hosts patched it in themselves with a global before_create on ActiveRecord::Base — a lot to ask of an app whose only deviation was not using bigints, and a hook broad enough to reach models that had nothing to do with OpenLoam.

Integer keys take the early return, so nothing changes for the default app.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
# File 'lib/open_loam/generated_key.rb', line 19

def self.included(base)
  base.before_create :assign_open_loam_generated_key
end