module Uuidify::UuidifyConcern
Mixin to add uuids to an active record model.
Public Instance Methods
uuid()
click to toggle source
Return a UUID, creating it if needed.
# 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.timestamp_create 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)
click to toggle source
Assign a UUID that came from an external source.
# 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
Private Instance Methods
abort_if_unsaved!()
click to toggle source
# File lib/uuidify/uuidify_concern.rb, line 72 def abort_if_unsaved! if self.id.nil? raise Uuidify::UuidifyException, "This model hasn't been persisted yet and Uuidify doesn't want to implicitly do that. Won't create uuid" end end
destroy_uuid()
click to toggle source
# File lib/uuidify/uuidify_concern.rb, line 78 def destroy_uuid model_name = self.class.to_s model_id = self.id uuid = Uuidify::Uuid.where(:model_name => model_name, :model_id => model_id).first uuid.destroy if uuid end