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' => '001',
30 'created_at' => '002',
31 'modified_at' => '003',
32 'modified_by_user_uuid' => '004',
33 'modified_by_client_uuid' => '005',
38 'updated_at' => 'zzz-999'
43 return @columns unless @columns.nil?
45 @attribute_info ||= {}
46 schema = $arvados_api_client.discovery[:schemas][self.to_s.to_sym]
47 return @columns if schema.nil?
48 schema[:properties].each do |k, coldef|
53 if coldef[:type] == coldef[:type].downcase
54 # boolean, integer, etc.
55 @columns << column(k, coldef[:type].to_sym)
58 @columns << column(k, :text)
59 serialize k, coldef[:type].constantize
62 @attribute_info[k] = coldef
68 def self.column(name, sql_type = nil, default = nil, null = true)
69 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
72 def self.attribute_info
77 def self.find(uuid, opts={})
78 if uuid.class != String or uuid.length < 27 then
79 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
82 # Only do one lookup on the API side per {class, uuid, workbench
83 # request} unless {cache: false} is given via opts.
84 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
85 if opts[:cache] == false
86 Rails.cache.write cache_key, $arvados_api_client.api(self, '/' + uuid)
88 hash = Rails.cache.fetch cache_key do
89 $arvados_api_client.api(self, '/' + uuid)
91 new.private_reload(hash)
95 ArvadosResourceList.new(self).order(*args)
98 def self.filter(*args)
99 ArvadosResourceList.new(self).filter(*args)
102 def self.where(*args)
103 ArvadosResourceList.new(self).where(*args)
106 def self.limit(*args)
107 ArvadosResourceList.new(self).limit(*args)
110 def self.eager(*args)
111 ArvadosResourceList.new(self).eager(*args)
115 ArvadosResourceList.new(self).all(*args)
120 self.class.columns.each do |col|
121 obdata[col.name.to_sym] = self.send(col.name.to_sym)
124 postdata = { self.class.to_s.underscore => obdata }
126 postdata['_method'] = 'PUT'
128 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
130 resp = $arvados_api_client.api(self.class, '', postdata)
132 return false if !resp[:etag] || !resp[:uuid]
134 # set read-only non-database attributes
138 # these attrs can be modified by "save" -- we should update our copies
139 %w(uuid owner_uuid created_at
140 modified_at modified_by_user_uuid modified_by_client_uuid
142 if self.respond_to? "#{attr}=".to_sym
143 self.send(attr + '=', resp[attr.to_sym])
153 self.save or raise Exception.new("Save failed")
158 postdata = { '_method' => 'DELETE' }
159 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
160 resp[:etag] && resp[:uuid] && resp
168 o.merge!(args.pop) if args[-1].is_a? Hash
169 o[:link_class] ||= args.shift
170 o[:name] ||= args.shift
171 o[:tail_uuid] = self.uuid
173 return all_links.select do |m|
178 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
186 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
187 @links = $arvados_api_client.unpack_api_response(@links)
191 return @all_links if @all_links
192 res = $arvados_api_client.api Link, '', {
195 tail_kind: self.kind,
200 @all_links = $arvados_api_client.unpack_api_response(res)
204 private_reload(self.uuid)
207 def private_reload(uuid_or_hash)
208 raise "No such object" if !uuid_or_hash
209 if uuid_or_hash.is_a? Hash
212 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
215 if self.respond_to?(k.to_s + '=')
216 self.send(k.to_s + '=', v)
218 # When ArvadosApiClient#schema starts telling us what to expect
219 # in API responses (not just the server side database
220 # columns), this sort of awfulness can be avoided:
221 self.instance_variable_set('@' + k.to_s, v)
222 if !self.respond_to? k
223 singleton = class << self; self end
224 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
241 def attributes_for_display
242 self.attributes.reject { |k,v|
243 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
245 attribute_sortkey[k] or k
254 (current_user and current_user.is_active and
255 (current_user.is_admin or
256 current_user.uuid == self.owner_uuid))
259 def attribute_editable?(attr)
260 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
262 elsif not (current_user.andand.is_active)
264 elsif "uuid owner_uuid".index(attr.to_s) or current_user.is_admin
265 current_user.is_admin
267 current_user.uuid == self.owner_uuid or current_user.uuid == self.uuid
271 def self.resource_class_for_uuid(uuid, opts={})
272 if uuid.is_a? ArvadosBase
275 unless uuid.is_a? String
278 if opts[:class].is_a? Class
281 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
285 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
286 resource_class ||= $arvados_api_client.
287 kind_class(self.uuid_infix_object_kind[re[1]])
289 if opts[:referring_object] and
290 opts[:referring_attr] and
291 opts[:referring_attr].match /_uuid$/
292 resource_class ||= $arvados_api_client.
293 kind_class(opts[:referring_object].
294 attributes[opts[:referring_attr].
295 sub(/_uuid$/, '_kind')])
300 def friendly_link_name
301 (name if self.respond_to? :name) || uuid
316 def self.current_user
317 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
318 Thread.current[:user]
321 self.class.current_user