17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/activerecord_uuid_as_pk.rb', line 17
def uuid_as_primary_key()
self.primary_key = "uuid"
define_method "formatted_id" do
return self.id.present? ? UUIDTools::UUID.parse_raw(self.id).to_s : nil
end
define_method "hex_id" do
return self.id.present? ? UUIDTools::UUID.parse_raw(self.id).hexdigest : nil
end
before_create do
if self.id.nil?
self.id = UUIDTools::UUID.timestamp_create.raw
elsif self.id.is_a?(UUIDTools::UUID)
self.id = self.id.raw
elsif self.id.is_a?(String)
if self.id.size != 16
if self.id =~ /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$/
self.id = UUIDTools::UUID.parse(self.id).raw
else
self.id = UUIDTools::UUID.timestamp_create.raw
end
end
else
self.id = UUIDTools::UUID.timestamp_create.raw
end
end
(class << self; self end).class_eval do
define_method "find_by_formatted_id" do |uuid|
raw_uuid = UUIDTools::UUID.parse(uuid).raw
return self.find_by_uuid(raw_uuid)
end
end
end
|