Module: Repia::Support::UUIDModel

Defined in:
lib/repia/support/uuid_model.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

Triggered when this module is included.



18
19
20
21
# File 'lib/repia/support/uuid_model.rb', line 18

def self.included(klass)
  klass.primary_key = "uuid"
  klass.before_create :generate_uuid
end

Instance Method Details

#generate_uuidObject

Generates an UUID for the model object.



26
27
28
# File 'lib/repia/support/uuid_model.rb', line 26

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