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',
56 @loaded_attributes = {}
60 return @columns if @columns.andand.any?
62 @attribute_info ||= {}
63 schema = arvados_api_client.discovery[:schemas][self.to_s.to_sym]
64 return @columns if schema.nil?
65 schema[:properties].each do |k, coldef|
70 if coldef[:type] == coldef[:type].downcase
71 # boolean, integer, etc.
72 @columns << column(k, coldef[:type].to_sym)
75 @columns << column(k, :text)
76 serialize k, coldef[:type].constantize
79 unless new_record? or @loaded_attributes.include? k.to_s
80 Rails.logger.debug "BUG: access non-loaded attribute #{k}"
82 # raise ActiveModel::MissingAttributeError, "missing attribute: #{k}"
86 @attribute_info[k] = coldef
92 def self.column(name, sql_type = nil, default = nil, null = true)
93 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
96 def self.attribute_info
101 def self.find(uuid, opts={})
102 if uuid.class != String or uuid.length < 27 then
103 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
106 if self == ArvadosBase
107 # Determine type from uuid and defer to the appropriate subclass.
108 return resource_class_for_uuid(uuid).find(uuid, opts)
111 # Only do one lookup on the API side per {class, uuid, workbench
112 # request} unless {cache: false} is given via opts.
113 cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
114 if opts[:cache] == false
115 Rails.cache.write cache_key, arvados_api_client.api(self, '/' + uuid)
117 hash = Rails.cache.fetch cache_key do
118 arvados_api_client.api(self, '/' + uuid)
120 new.private_reload(hash)
123 def self.find?(*args)
124 find(*args) rescue nil
127 def self.order(*args)
128 ArvadosResourceList.new(self).order(*args)
131 def self.filter(*args)
132 ArvadosResourceList.new(self).filter(*args)
135 def self.where(*args)
136 ArvadosResourceList.new(self).where(*args)
139 def self.limit(*args)
140 ArvadosResourceList.new(self).limit(*args)
143 def self.select(*args)
144 ArvadosResourceList.new(self).select(*args)
147 def self.distinct(*args)
148 ArvadosResourceList.new(self).distinct(*args)
151 def self.eager(*args)
152 ArvadosResourceList.new(self).eager(*args)
156 ArvadosResourceList.new(self)
159 def self.permit_attribute_params raw_params
160 # strong_parameters does not provide security in Workbench: anyone
161 # who can get this far can just as well do a call directly to our
162 # database (Arvados) with the same credentials we use.
164 # The following permit! is necessary even with
165 # "ActionController::Parameters.permit_all_parameters = true",
166 # because permit_all does not permit nested attributes.
167 ActionController::Parameters.new(raw_params).permit!
170 def self.create raw_params={}, create_params={}
171 x = super(permit_attribute_params(raw_params))
172 x.create_params = create_params
176 def update_attributes raw_params={}
177 super(self.class.permit_attribute_params(raw_params))
182 self.class.columns.each do |col|
183 # Non-nil serialized values must be sent because we can't tell
184 # whether they've changed. Other than that, any given attribute
185 # is either unchanged (in which case there's no need to send its
186 # old value in the update/create command) or has been added to
187 # #changed by ActiveRecord's #attr= method.
188 if changed.include? col.name or
189 (self.class.serialized_attributes.include? col.name and
190 @loaded_attributes[col.name])
191 obdata[col.name.to_sym] = self.send col.name
195 postdata = { self.class.to_s.underscore => obdata }
197 postdata['_method'] = 'PUT'
199 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
201 postdata.merge!(@create_params) if @create_params
202 resp = arvados_api_client.api(self.class, '', postdata)
204 return false if !resp[:etag] || !resp[:uuid]
206 # set read-only non-database attributes
210 # attributes can be modified during "save" -- we should update our copies
211 resp.keys.each do |attr|
212 if self.respond_to? "#{attr}=".to_sym
213 self.send(attr.to_s + '=', resp[attr.to_sym])
224 self.save or raise Exception.new("Save failed")
229 postdata = { '_method' => 'DELETE' }
230 resp = arvados_api_client.api(self.class, '/' + uuid, postdata)
231 resp[:etag] && resp[:uuid] && resp
239 o.merge!(args.pop) if args[-1].is_a? Hash
240 o[:link_class] ||= args.shift
241 o[:name] ||= args.shift
242 o[:tail_uuid] = self.uuid
244 return all_links.select do |m|
249 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
257 @links = arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
258 @links = arvados_api_client.unpack_api_response(@links)
262 return @all_links if @all_links
263 res = arvados_api_client.api Link, '', {
266 tail_kind: self.kind,
271 @all_links = arvados_api_client.unpack_api_response(res)
275 private_reload(self.uuid)
278 def private_reload(uuid_or_hash)
279 raise "No such object" if !uuid_or_hash
280 if uuid_or_hash.is_a? Hash
283 hash = arvados_api_client.api(self.class, '/' + uuid_or_hash)
286 @loaded_attributes[k.to_s] = true
287 if self.respond_to?(k.to_s + '=')
288 self.send(k.to_s + '=', v)
290 # When ArvadosApiClient#schema starts telling us what to expect
291 # in API responses (not just the server side database
292 # columns), this sort of awfulness can be avoided:
293 self.instance_variable_set('@' + k.to_s, v)
294 if !self.respond_to? k
295 singleton = class << self; self end
296 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
310 def initialize_copy orig
315 def attributes_for_display
316 self.attributes.reject { |k,v|
317 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
319 attribute_sortkey[k] or k
323 def class_for_display
324 self.class.to_s.underscore.humanize
327 def self.class_for_display
328 self.to_s.underscore.humanize
331 # Array of strings that are names of attributes that should be rendered as textile.
332 def textile_attributes
337 current_user.andand.is_active
340 def self.goes_in_projects?
344 # can this class of object be copied into a project?
345 # override to false on indivudal model classes for which this should not be true
346 def self.copies_to_projects?
347 self.goes_in_projects?
351 (current_user and current_user.is_active and
352 (current_user.is_admin or
353 current_user.uuid == self.owner_uuid or
355 (respond_to?(:writable_by) ?
356 writable_by.include?(current_user.uuid) :
357 (ArvadosBase.find(owner_uuid).writable_by.include? current_user.uuid rescue false)))) or false
364 # Array of strings that are the names of attributes that can be edited
366 def editable_attributes
367 self.class.columns.map(&:name) -
368 %w(created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at)
371 def attribute_editable?(attr, ever=nil)
372 if not editable_attributes.include?(attr.to_s)
374 elsif not (current_user.andand.is_active)
377 current_user.is_admin
385 def self.resource_class_for_uuid(uuid, opts={})
386 if uuid.is_a? ArvadosBase
389 unless uuid.is_a? String
392 if opts[:class].is_a? Class
395 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
399 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
400 resource_class ||= arvados_api_client.
401 kind_class(self.uuid_infix_object_kind[re[1]])
403 if opts[:referring_object] and
404 opts[:referring_attr] and
405 opts[:referring_attr].match /_uuid$/
406 resource_class ||= arvados_api_client.
407 kind_class(opts[:referring_object].
408 attributes[opts[:referring_attr].
409 sub(/_uuid$/, '_kind')])
414 def resource_param_name
415 self.class.to_s.underscore
418 def friendly_link_name lookup=nil
419 (name if self.respond_to? :name) || default_name
423 self.class_for_display
430 def self.default_name
431 self.to_s.underscore.humanize
435 (self.class.to_s.pluralize + 'Controller').constantize
439 self.class.to_s.tableize
442 # Placeholder for name when name is missing or empty
444 if self.respond_to? :name
445 "New #{class_for_display.downcase}"
452 ArvadosBase.find(owner_uuid) rescue nil
463 def self.current_user
464 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
465 Thread.current[:user]
468 self.class.current_user