Module: RailsIdentity::UUIDModel

Included in:
Session, User
Defined in:
lib/rails_identity.rb

Overview

This module is a mixin that allows the model to use UUIDs instead of normal IDs. By including this module, the model class declares that the primary key is called “uuid” and an UUID is generated right before save(). You may assign an UUID prior to save, in which case, no new UUID will be generated.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



35
36
37
38
39
40
# File 'lib/rails_identity.rb', line 35

def self.included(klass)
  # Triggered when this module is included.

  klass.primary_key = "uuid"
  klass.before_create :generate_uuid
end

Instance Method Details

#generate_uuidObject



42
43
44
45
46
47
48
49
# File 'lib/rails_identity.rb', line 42

def generate_uuid()
  # Generates an UUID for the model object only if it hasn't been assigned
  # one yet.

  if self.uuid.nil?
    self.uuid = UUIDTools::UUID.timestamp_create().to_s
  end
end