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 # Only do one lookup on the API side per {class, uuid, workbench
89 # request} unless {cache: false} is given via opts.
90 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
91 if opts[:cache] == false
92 Rails.cache.write cache_key, $arvados_api_client.api(self, '/' + uuid)
94 hash = Rails.cache.fetch cache_key do
95 $arvados_api_client.api(self, '/' + uuid)
97 new.private_reload(hash)
100 def self.order(*args)
101 ArvadosResourceList.new(self).order(*args)
104 def self.filter(*args)
105 ArvadosResourceList.new(self).filter(*args)
108 def self.where(*args)
109 ArvadosResourceList.new(self).where(*args)
112 def self.limit(*args)
113 ArvadosResourceList.new(self).limit(*args)
116 def self.eager(*args)
117 ArvadosResourceList.new(self).eager(*args)
121 ArvadosResourceList.new(self).all(*args)
126 self.class.columns.each do |col|
127 obdata[col.name.to_sym] = self.send(col.name.to_sym)
130 postdata = { self.class.to_s.underscore => obdata }
132 postdata['_method'] = 'PUT'
134 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
136 resp = $arvados_api_client.api(self.class, '', postdata)
138 return false if !resp[:etag] || !resp[:uuid]
140 # set read-only non-database attributes
144 # attributes can be modified during "save" -- we should update our copies
145 resp.keys.each do |attr|
146 if self.respond_to? "#{attr}=".to_sym
147 self.send(attr.to_s + '=', resp[attr.to_sym])
157 self.save or raise Exception.new("Save failed")
162 postdata = { '_method' => 'DELETE' }
163 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
164 resp[:etag] && resp[:uuid] && resp
172 o.merge!(args.pop) if args[-1].is_a? Hash
173 o[:link_class] ||= args.shift
174 o[:name] ||= args.shift
175 o[:tail_uuid] = self.uuid
177 return all_links.select do |m|
182 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
190 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
191 @links = $arvados_api_client.unpack_api_response(@links)
195 return @all_links if @all_links
196 res = $arvados_api_client.api Link, '', {
199 tail_kind: self.kind,
204 @all_links = $arvados_api_client.unpack_api_response(res)
208 private_reload(self.uuid)
211 def private_reload(uuid_or_hash)
212 raise "No such object" if !uuid_or_hash
213 if uuid_or_hash.is_a? Hash
216 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
219 if self.respond_to?(k.to_s + '=')
220 self.send(k.to_s + '=', v)
222 # When ArvadosApiClient#schema starts telling us what to expect
223 # in API responses (not just the server side database
224 # columns), this sort of awfulness can be avoided:
225 self.instance_variable_set('@' + k.to_s, v)
226 if !self.respond_to? k
227 singleton = class << self; self end
228 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
245 def attributes_for_display
246 self.attributes.reject { |k,v|
247 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
249 attribute_sortkey[k] or k
253 def class_for_display
262 (current_user and current_user.is_active and
263 (current_user.is_admin or
264 current_user.uuid == self.owner_uuid))
267 def attribute_editable?(attr)
268 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
270 elsif not (current_user.andand.is_active)
272 elsif "uuid owner_uuid".index(attr.to_s) or current_user.is_admin
273 current_user.is_admin
275 current_user.uuid == self.owner_uuid or current_user.uuid == self.uuid
279 def self.resource_class_for_uuid(uuid, opts={})
280 if uuid.is_a? ArvadosBase
283 unless uuid.is_a? String
286 if opts[:class].is_a? Class
289 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
293 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
294 resource_class ||= $arvados_api_client.
295 kind_class(self.uuid_infix_object_kind[re[1]])
297 if opts[:referring_object] and
298 opts[:referring_attr] and
299 opts[:referring_attr].match /_uuid$/
300 resource_class ||= $arvados_api_client.
301 kind_class(opts[:referring_object].
302 attributes[opts[:referring_attr].
303 sub(/_uuid$/, '_kind')])
308 def friendly_link_name
309 (name if self.respond_to? :name) || uuid
313 self.class_for_display
328 def self.current_user
329 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
330 Thread.current[:user]
333 self.class.current_user