1 class ArvadosBase < ActiveRecord::Base
2 self.abstract_class = true
3 attr_accessor :attribute_sortkey
5 def self.uuid_infix_object_kind
6 @@uuid_infix_object_kind ||=
9 $arvados_api_client.discovery[:schemas].each do |name, schema|
10 if schema[:uuidPrefix]
11 infix_kind[schema[:uuidPrefix]] =
12 'arvados#' + name.to_s.camelcase(:lower)
16 # Recognize obsolete types.
18 merge('mxsvm' => 'arvados#pipelineTemplate', # Pipeline
19 'uo14g' => 'arvados#pipelineInstance', # PipelineInvocation
20 'ldvyl' => 'arvados#group') # Project
26 @attribute_sortkey ||= {
29 'owner_uuid' => '002',
30 'event_type' => '100',
31 'link_class' => '100',
32 'group_class' => '100',
35 'object_uuid' => '102',
37 'description' => '104',
38 'properties' => '150',
40 'created_at' => '200',
41 'modified_at' => '201',
42 'modified_by_user_uuid' => '202',
43 'modified_by_client_uuid' => '203',
49 return @columns unless @columns.nil?
51 @attribute_info ||= {}
52 schema = $arvados_api_client.discovery[:schemas][self.to_s.to_sym]
53 return @columns if schema.nil?
54 schema[:properties].each do |k, coldef|
59 if coldef[:type] == coldef[:type].downcase
60 # boolean, integer, etc.
61 @columns << column(k, coldef[:type].to_sym)
64 @columns << column(k, :text)
65 serialize k, coldef[:type].constantize
68 @attribute_info[k] = coldef
74 def self.column(name, sql_type = nil, default = nil, null = true)
75 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
78 def self.attribute_info
83 def self.find(uuid, opts={})
84 if uuid.class != String or uuid.length < 27 then
85 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
88 if self == ArvadosBase
89 # Determine type from uuid and defer to the appropriate subclass.
90 return resource_class_for_uuid(uuid).find(uuid, opts)
93 # Only do one lookup on the API side per {class, uuid, workbench
94 # request} unless {cache: false} is given via opts.
95 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
96 if opts[:cache] == false
97 Rails.cache.write cache_key, $arvados_api_client.api(self, '/' + uuid)
99 hash = Rails.cache.fetch cache_key do
100 $arvados_api_client.api(self, '/' + uuid)
102 new.private_reload(hash)
105 def self.order(*args)
106 ArvadosResourceList.new(self).order(*args)
109 def self.filter(*args)
110 ArvadosResourceList.new(self).filter(*args)
113 def self.where(*args)
114 ArvadosResourceList.new(self).where(*args)
117 def self.limit(*args)
118 ArvadosResourceList.new(self).limit(*args)
121 def self.eager(*args)
122 ArvadosResourceList.new(self).eager(*args)
126 ArvadosResourceList.new(self).all(*args)
131 self.class.columns.each do |col|
132 obdata[col.name.to_sym] = self.send(col.name.to_sym)
135 postdata = { self.class.to_s.underscore => obdata }
137 postdata['_method'] = 'PUT'
139 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
141 resp = $arvados_api_client.api(self.class, '', postdata)
143 return false if !resp[:etag] || !resp[:uuid]
145 # set read-only non-database attributes
149 # attributes can be modified during "save" -- we should update our copies
150 resp.keys.each do |attr|
151 if self.respond_to? "#{attr}=".to_sym
152 self.send(attr.to_s + '=', resp[attr.to_sym])
162 self.save or raise Exception.new("Save failed")
167 postdata = { '_method' => 'DELETE' }
168 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
169 resp[:etag] && resp[:uuid] && resp
177 o.merge!(args.pop) if args[-1].is_a? Hash
178 o[:link_class] ||= args.shift
179 o[:name] ||= args.shift
180 o[:tail_uuid] = self.uuid
182 return all_links.select do |m|
187 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
195 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
196 @links = $arvados_api_client.unpack_api_response(@links)
200 return @all_links if @all_links
201 res = $arvados_api_client.api Link, '', {
204 tail_kind: self.kind,
209 @all_links = $arvados_api_client.unpack_api_response(res)
213 private_reload(self.uuid)
216 def private_reload(uuid_or_hash)
217 raise "No such object" if !uuid_or_hash
218 if uuid_or_hash.is_a? Hash
221 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
224 if self.respond_to?(k.to_s + '=')
225 self.send(k.to_s + '=', v)
227 # When ArvadosApiClient#schema starts telling us what to expect
228 # in API responses (not just the server side database
229 # columns), this sort of awfulness can be avoided:
230 self.instance_variable_set('@' + k.to_s, v)
231 if !self.respond_to? k
232 singleton = class << self; self end
233 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
250 def attributes_for_display
251 self.attributes.reject { |k,v|
252 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
254 attribute_sortkey[k] or k
258 def class_for_display
267 (current_user and current_user.is_active and
268 (current_user.is_admin or
269 current_user.uuid == self.owner_uuid or
273 def attribute_editable?(attr)
274 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
276 elsif not (current_user.andand.is_active)
278 elsif "uuid owner_uuid".index(attr.to_s) or current_user.is_admin
279 current_user.is_admin
281 current_user.uuid == self.owner_uuid or
282 current_user.uuid == self.uuid or
287 def self.resource_class_for_uuid(uuid, opts={})
288 if uuid.is_a? ArvadosBase
291 unless uuid.is_a? String
294 if opts[:class].is_a? Class
297 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
301 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
302 resource_class ||= $arvados_api_client.
303 kind_class(self.uuid_infix_object_kind[re[1]])
305 if opts[:referring_object] and
306 opts[:referring_attr] and
307 opts[:referring_attr].match /_uuid$/
308 resource_class ||= $arvados_api_client.
309 kind_class(opts[:referring_object].
310 attributes[opts[:referring_attr].
311 sub(/_uuid$/, '_kind')])
316 def friendly_link_name
317 (name if self.respond_to? :name) || uuid
321 self.class_for_display
336 def self.current_user
337 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
338 Thread.current[:user]
341 self.class.current_user