Module: Uuidify::UuidifyConcern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/uuidify/uuidify_concern.rb
Overview
Mixin to add uuids to an active record model.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#uuid ⇒ Object
Return a UUID, creating it if needed.
-
#uuid=(new_uuid) ⇒ Object
Assign a UUID that came from an external source.
Instance Method Details
#uuid ⇒ Object
Return a UUID, creating it if needed.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/uuidify/uuidify_concern.rb', line 41 def uuid abort_if_unsaved! model_name = self.class.to_s model_id = self.id uuid = Uuidify::Uuid.where(:model_name => model_name, :model_id => model_id).first if uuid.nil? new_uuid = UUIDTools::UUID. new_uuid = Uuidify::Uuid.uuid_to_sql_string(new_uuid) uuid = Uuidify::Uuid.create(:model_name => model_name, :model_id => self.id, :model_uuid => new_uuid) uuid.save! end Uuidify::Uuid.uuid_from_sql_string(uuid.model_uuid) end |
#uuid=(new_uuid) ⇒ Object
Assign a UUID that came from an external source.
62 63 64 65 66 67 68 |
# File 'lib/uuidify/uuidify_concern.rb', line 62 def uuid= new_uuid abort_if_unsaved! uuid_value = Uuidify::Uuid.uuid_to_sql_string(new_uuid) Uuidify::Uuid.where(:model_name => self.class.to_s, :model_id => self.id).first_or_create!.update_column(:model_uuid, uuid_value) end |