1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class ArvadosBase < ActiveRecord::Base
6 self.abstract_class = true
7 attr_accessor :attribute_sortkey
8 attr_accessor :create_params
10 def self.arvados_api_client
11 ArvadosApiClient.new_or_current
14 def arvados_api_client
15 ArvadosApiClient.new_or_current
18 def self.uuid_infix_object_kind
19 @@uuid_infix_object_kind ||=
22 arvados_api_client.discovery[:schemas].each do |name, schema|
23 if schema[:uuidPrefix]
24 infix_kind[schema[:uuidPrefix]] =
25 'arvados#' + name.to_s.camelcase(:lower)
29 # Recognize obsolete types.
31 merge('mxsvm' => 'arvados#pipelineTemplate', # Pipeline
32 'uo14g' => 'arvados#pipelineInstance', # PipelineInvocation
33 'ldvyl' => 'arvados#group') # Project
37 def initialize raw_params={}, create_params={}
38 super self.class.permit_attribute_params(raw_params)
39 @create_params = create_params
40 @attribute_sortkey ||= {
43 'owner_uuid' => '002',
44 'event_type' => '100',
45 'link_class' => '100',
46 'group_class' => '100',
49 'object_uuid' => '102',
51 'description' => '104',
52 'properties' => '150',
54 'created_at' => '200',
55 'modified_at' => '201',
56 'modified_by_user_uuid' => '202',
57 'modified_by_client_uuid' => '203',
60 @loaded_attributes = {}
64 return @columns if @columns.andand.any?
66 @attribute_info ||= {}
67 schema = arvados_api_client.discovery[:schemas][self.to_s.to_sym]
68 return @columns if schema.nil?
69 schema[:properties].each do |k, coldef|
74 if coldef[:type] == coldef[:type].downcase
75 # boolean, integer, etc.
76 @columns << column(k, coldef[:type].to_sym)
79 @columns << column(k, :text)
80 serialize k, coldef[:type].constantize
83 unless new_record? or @loaded_attributes.include? k.to_s
84 Rails.logger.debug "BUG: access non-loaded attribute #{k}"
86 # raise ActiveModel::MissingAttributeError, "missing attribute: #{k}"
90 @attribute_info[k] = coldef
96 def self.column(name, sql_type = nil, default = nil, null = true)
97 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
100 def self.attribute_info
105 def self.find(uuid, opts={})
106 if uuid.class != String or uuid.length < 27 then
107 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
110 if self == ArvadosBase
111 # Determine type from uuid and defer to the appropriate subclass.
112 return resource_class_for_uuid(uuid).find(uuid, opts)
115 # Only do one lookup on the API side per {class, uuid, workbench
116 # request} unless {cache: false} is given via opts.
117 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
118 if opts[:cache] == false
119 Rails.cache.write cache_key, arvados_api_client.api(self, '/' + uuid)
121 hash = Rails.cache.fetch cache_key do
122 arvados_api_client.api(self, '/' + uuid)
124 new.private_reload(hash)
127 def self.find?(*args)
128 find(*args) rescue nil
131 def self.order(*args)
132 ArvadosResourceList.new(self).order(*args)
135 def self.filter(*args)
136 ArvadosResourceList.new(self).filter(*args)
139 def self.where(*args)
140 ArvadosResourceList.new(self).where(*args)
143 def self.limit(*args)
144 ArvadosResourceList.new(self).limit(*args)
147 def self.select(*args)
148 ArvadosResourceList.new(self).select(*args)
151 def self.with_count(*args)
152 ArvadosResourceList.new(self).with_count(*args)
155 def self.distinct(*args)
156 ArvadosResourceList.new(self).distinct(*args)
159 def self.include_trash(*args)
160 ArvadosResourceList.new(self).include_trash(*args)
163 def self.recursive(*args)
164 ArvadosResourceList.new(self).recursive(*args)
167 def self.eager(*args)
168 ArvadosResourceList.new(self).eager(*args)
172 ArvadosResourceList.new(self)
175 def self.permit_attribute_params raw_params
176 # strong_parameters does not provide security in Workbench: anyone
177 # who can get this far can just as well do a call directly to our
178 # database (Arvados) with the same credentials we use.
180 # The following permit! is necessary even with
181 # "ActionController::Parameters.permit_all_parameters = true",
182 # because permit_all does not permit nested attributes.
183 ActionController::Parameters.new(raw_params).permit!
186 def self.create raw_params={}, create_params={}
187 x = super(permit_attribute_params(raw_params))
188 x.create_params = create_params
192 def update_attributes raw_params={}
193 super(self.class.permit_attribute_params(raw_params))
198 self.class.columns.each do |col|
199 # Non-nil serialized values must be sent because we can't tell
200 # whether they've changed. Other than that, any given attribute
201 # is either unchanged (in which case there's no need to send its
202 # old value in the update/create command) or has been added to
203 # #changed by ActiveRecord's #attr= method.
204 if changed.include? col.name or
205 (self.class.serialized_attributes.include? col.name and
206 @loaded_attributes[col.name])
207 obdata[col.name.to_sym] = self.send col.name
211 postdata = { self.class.to_s.underscore => obdata }
213 postdata['_method'] = 'PUT'
215 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
217 postdata.merge!(@create_params) if @create_params
218 resp = arvados_api_client.api(self.class, '', postdata)
220 return false if !resp[:etag] || !resp[:uuid]
222 # set read-only non-database attributes
226 # attributes can be modified during "save" -- we should update our copies
227 resp.keys.each do |attr|
228 if self.respond_to? "#{attr}=".to_sym
229 self.send(attr.to_s + '=', resp[attr.to_sym])
240 self.save or raise Exception.new("Save failed")
245 postdata = { '_method' => 'DELETE' }
246 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
247 resp[:etag] && resp[:uuid] && resp
255 o.merge!(args.pop) if args[-1].is_a? Hash
256 o[:link_class] ||= args.shift
257 o[:name] ||= args.shift
258 o[:tail_uuid] = self.uuid
260 return all_links.select do |m|
265 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
273 @links = arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
274 @links = arvados_api_client.unpack_api_response(@links)
278 return @all_links if @all_links
279 res = arvados_api_client.api Link, '', {
282 tail_kind: self.kind,
287 @all_links = arvados_api_client.unpack_api_response(res)
291 private_reload(self.uuid)
294 def private_reload(uuid_or_hash)
295 raise "No such object" if !uuid_or_hash
296 if uuid_or_hash.is_a? Hash
299 hash = arvados_api_client.api(self.class, '/' + uuid_or_hash)
302 @loaded_attributes[k.to_s] = true
303 if self.respond_to?(k.to_s + '=')
304 self.send(k.to_s + '=', v)
306 # When ArvadosApiClient#schema starts telling us what to expect
307 # in API responses (not just the server side database
308 # columns), this sort of awfulness can be avoided:
309 self.instance_variable_set('@' + k.to_s, v)
310 if !self.respond_to? k
311 singleton = class << self; self end
312 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
326 def initialize_copy orig
331 def attributes_for_display
332 self.attributes.reject { |k,v|
333 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
335 attribute_sortkey[k] or k
339 def class_for_display
340 self.class.to_s.underscore.humanize
343 def self.class_for_display
344 self.to_s.underscore.humanize
347 # Array of strings that are names of attributes that should be rendered as textile.
348 def textile_attributes
353 current_user.andand.is_active && api_exists?(:create)
356 def self.goes_in_projects?
360 # can this class of object be copied into a project?
361 # override to false on indivudal model classes for which this should not be true
362 def self.copies_to_projects?
363 self.goes_in_projects?
367 (current_user and current_user.is_active and
368 (current_user.is_admin or
369 current_user.uuid == self.owner_uuid or
371 (respond_to?(:writable_by) ?
372 writable_by.include?(current_user.uuid) :
373 (ArvadosBase.find(owner_uuid).writable_by.include? current_user.uuid rescue false)))) or false
380 def self.api_exists?(method)
381 arvados_api_client.discovery[:resources][self.to_s.underscore.pluralize.to_sym].andand[:methods].andand[method]
384 # Array of strings that are the names of attributes that can be edited
386 def editable_attributes
387 self.class.columns.map(&:name) -
388 %w(created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at)
391 def attribute_editable?(attr, ever=nil)
392 if not editable_attributes.include?(attr.to_s)
394 elsif not (current_user.andand.is_active)
397 current_user.is_admin
405 def self.resource_class_for_uuid(uuid, opts={})
406 if uuid.is_a? ArvadosBase
409 unless uuid.is_a? String
412 if opts[:class].is_a? Class
415 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
419 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
420 resource_class ||= arvados_api_client.
421 kind_class(self.uuid_infix_object_kind[re[1]])
423 if opts[:referring_object] and
424 opts[:referring_attr] and
425 opts[:referring_attr].match /_uuid$/
426 resource_class ||= arvados_api_client.
427 kind_class(opts[:referring_object].
428 attributes[opts[:referring_attr].
429 sub(/_uuid$/, '_kind')])
434 def resource_param_name
435 self.class.to_s.underscore
438 def friendly_link_name lookup=nil
439 (name if self.respond_to? :name) || default_name
443 self.class_for_display
450 def self.default_name
451 self.to_s.underscore.humanize
455 (self.class.to_s.pluralize + 'Controller').constantize
459 self.class.to_s.tableize
462 # Placeholder for name when name is missing or empty
464 if self.respond_to? :name
465 "New #{class_for_display.downcase}"
472 ArvadosBase.find(owner_uuid) rescue nil
483 def self.current_user
484 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
485 Thread.current[:user]
488 self.class.current_user