1 class ArvadosBase < ActiveRecord::Base
2 self.abstract_class = true
3 attr_accessor :attribute_sortkey
4 attr_accessor :create_params
6 def self.arvados_api_client
7 ArvadosApiClient.new_or_current
10 def arvados_api_client
11 ArvadosApiClient.new_or_current
14 def self.uuid_infix_object_kind
15 @@uuid_infix_object_kind ||=
18 arvados_api_client.discovery[:schemas].each do |name, schema|
19 if schema[:uuidPrefix]
20 infix_kind[schema[:uuidPrefix]] =
21 'arvados#' + name.to_s.camelcase(:lower)
25 # Recognize obsolete types.
27 merge('mxsvm' => 'arvados#pipelineTemplate', # Pipeline
28 'uo14g' => 'arvados#pipelineInstance', # PipelineInvocation
29 'ldvyl' => 'arvados#group') # Project
33 def initialize raw_params={}, create_params={}
34 super self.class.permit_attribute_params(raw_params)
35 @create_params = create_params
36 @attribute_sortkey ||= {
39 'owner_uuid' => '002',
40 'event_type' => '100',
41 'link_class' => '100',
42 'group_class' => '100',
45 'object_uuid' => '102',
47 'description' => '104',
48 'properties' => '150',
50 'created_at' => '200',
51 'modified_at' => '201',
52 'modified_by_user_uuid' => '202',
53 'modified_by_client_uuid' => '203',
59 return @columns unless @columns.nil?
61 @attribute_info ||= {}
62 schema = arvados_api_client.discovery[:schemas][self.to_s.to_sym]
63 return @columns if schema.nil?
64 schema[:properties].each do |k, coldef|
69 if coldef[:type] == coldef[:type].downcase
70 # boolean, integer, etc.
71 @columns << column(k, coldef[:type].to_sym)
74 @columns << column(k, :text)
75 serialize k, coldef[:type].constantize
77 @attribute_info[k] = coldef
83 def self.column(name, sql_type = nil, default = nil, null = true)
84 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
87 def self.attribute_info
92 def self.find(uuid, opts={})
93 if uuid.class != String or uuid.length < 27 then
94 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
97 if self == ArvadosBase
98 # Determine type from uuid and defer to the appropriate subclass.
99 return resource_class_for_uuid(uuid).find(uuid, opts)
102 # Only do one lookup on the API side per {class, uuid, workbench
103 # request} unless {cache: false} is given via opts.
104 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
105 if opts[:cache] == false
106 Rails.cache.write cache_key, arvados_api_client.api(self, '/' + uuid)
108 hash = Rails.cache.fetch cache_key do
109 arvados_api_client.api(self, '/' + uuid)
111 new.private_reload(hash)
114 def self.order(*args)
115 ArvadosResourceList.new(self).order(*args)
118 def self.filter(*args)
119 ArvadosResourceList.new(self).filter(*args)
122 def self.where(*args)
123 ArvadosResourceList.new(self).where(*args)
126 def self.limit(*args)
127 ArvadosResourceList.new(self).limit(*args)
130 def self.eager(*args)
131 ArvadosResourceList.new(self).eager(*args)
135 ArvadosResourceList.new(self).all(*args)
138 def self.permit_attribute_params raw_params
139 # strong_parameters does not provide security in Workbench: anyone
140 # who can get this far can just as well do a call directly to our
141 # database (Arvados) with the same credentials we use.
143 # The following permit! is necessary even with
144 # "ActionController::Parameters.permit_all_parameters = true",
145 # because permit_all does not permit nested attributes.
146 ActionController::Parameters.new(raw_params).permit!
149 def self.create raw_params={}, create_params={}
150 x = super(permit_attribute_params(raw_params))
151 x.create_params = create_params
155 def update_attributes raw_params={}
156 super(self.class.permit_attribute_params(raw_params))
161 self.class.columns.each do |col|
162 obdata[col.name.to_sym] = self.send(col.name.to_sym)
165 postdata = { self.class.to_s.underscore => obdata }
167 postdata['_method'] = 'PUT'
169 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
171 postdata.merge!(@create_params) if @create_params
172 resp = arvados_api_client.api(self.class, '', postdata)
174 return false if !resp[:etag] || !resp[:uuid]
176 # set read-only non-database attributes
180 # attributes can be modified during "save" -- we should update our copies
181 resp.keys.each do |attr|
182 if self.respond_to? "#{attr}=".to_sym
183 self.send(attr.to_s + '=', resp[attr.to_sym])
193 self.save or raise Exception.new("Save failed")
198 postdata = { '_method' => 'DELETE' }
199 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
200 resp[:etag] && resp[:uuid] && resp
208 o.merge!(args.pop) if args[-1].is_a? Hash
209 o[:link_class] ||= args.shift
210 o[:name] ||= args.shift
211 o[:tail_uuid] = self.uuid
213 return all_links.select do |m|
218 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
226 @links = arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
227 @links = arvados_api_client.unpack_api_response(@links)
231 return @all_links if @all_links
232 res = arvados_api_client.api Link, '', {
235 tail_kind: self.kind,
240 @all_links = arvados_api_client.unpack_api_response(res)
244 private_reload(self.uuid)
247 def private_reload(uuid_or_hash)
248 raise "No such object" if !uuid_or_hash
249 if uuid_or_hash.is_a? Hash
252 hash = arvados_api_client.api(self.class, '/' + uuid_or_hash)
255 if self.respond_to?(k.to_s + '=')
256 self.send(k.to_s + '=', v)
258 # When ArvadosApiClient#schema starts telling us what to expect
259 # in API responses (not just the server side database
260 # columns), this sort of awfulness can be avoided:
261 self.instance_variable_set('@' + k.to_s, v)
262 if !self.respond_to? k
263 singleton = class << self; self end
264 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
281 def attributes_for_display
282 self.attributes.reject { |k,v|
283 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
285 attribute_sortkey[k] or k
289 def class_for_display
297 def self.goes_in_folders?
302 (current_user and current_user.is_active and
303 (current_user.is_admin or
304 current_user.uuid == self.owner_uuid or
306 (writable_by.include? current_user.uuid rescue false)))
309 def attribute_editable?(attr)
310 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
312 elsif not (current_user.andand.is_active)
315 current_user.is_admin
321 def self.resource_class_for_uuid(uuid, opts={})
322 if uuid.is_a? ArvadosBase
325 unless uuid.is_a? String
328 if opts[:class].is_a? Class
331 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
335 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
336 resource_class ||= arvados_api_client.
337 kind_class(self.uuid_infix_object_kind[re[1]])
339 if opts[:referring_object] and
340 opts[:referring_attr] and
341 opts[:referring_attr].match /_uuid$/
342 resource_class ||= arvados_api_client.
343 kind_class(opts[:referring_object].
344 attributes[opts[:referring_attr].
345 sub(/_uuid$/, '_kind')])
350 def friendly_link_name
351 (name if self.respond_to? :name) || uuid
355 self.class_for_display
363 ArvadosBase.find(owner_uuid) rescue nil
374 def self.current_user
375 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
376 Thread.current[:user]
379 self.class.current_user