Reload the target object before updating if it is marked read-only
[arvados.git] / services / api / lib / assign_uuid.rb
1 module AssignUuid
2
3   def self.included(base)
4     base.extend(ClassMethods)
5     base.before_create :assign_uuid
6   end
7
8   module ClassMethods
9     def uuid_prefix
10       Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1]
11     end
12   end
13
14   protected
15
16   def respond_to_uuid?
17     self.respond_to? :uuid
18   end
19
20   def assign_uuid
21     return true if !self.respond_to_uuid?
22     return true if uuid and current_user and current_user.is_admin
23     self.uuid = [Server::Application.config.uuid_prefix,
24                  self.class.uuid_prefix,
25                  rand(2**256).to_s(36)[-15..-1]].
26       join '-'
27   end
28 end