Module: RailsIdentity::UUIDModel
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
36 37 38 39 40 41 |
# File 'lib/rails_identity.rb', line 36 def self.included(klass) # Triggered when this module is included. klass.primary_key = "uuid" klass.before_create :generate_uuid end |
Instance Method Details
#generate_uuid ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/rails_identity.rb', line 43 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.().to_s end end |