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 ||= {
7 '4zz18' => 'arvados#collection',
8 'tpzed' => 'arvados#user',
9 'ozdt8' => 'arvados#api_client',
10 '8i9sb' => 'arvados#job',
11 'o0j2j' => 'arvados#link',
12 '57u5n' => 'arvados#log',
13 'j58dm' => 'arvados#specimen',
14 'p5p6p' => 'arvados#pipeline_template',
15 'mxsvm' => 'arvados#pipeline_template', # legacy Pipeline objects
16 'd1hrv' => 'arvados#pipeline_instance',
17 'uo14g' => 'arvados#pipeline_instance', # legacy PipelineInstance objects
18 'j7d0g' => 'arvados#group',
19 'ldvyl' => 'arvados#group' # only needed for legacy Project objects
25 @attribute_sortkey ||= {
29 'created_at' => '002',
30 'modified_at' => '003',
31 'modified_by_user' => '004',
32 'modified_by_client' => '005',
39 'updated_at' => 'zzz-999'
44 return @columns unless @columns.nil?
46 @attribute_info ||= {}
47 return @columns if $arvados_api_client.arvados_schema[self.to_s.to_sym].nil?
48 $arvados_api_client.arvados_schema[self.to_s.to_sym].each do |coldef|
49 k = coldef[:name].to_sym
50 if coldef[:type] == coldef[:type].downcase
51 @columns << column(k, coldef[:type].to_sym)
53 @columns << column(k, :text)
54 serialize k, coldef[:type].constantize
57 @attribute_info[k] = coldef
63 def self.column(name, sql_type = nil, default = nil, null = true)
64 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
66 def self.attribute_info
71 if uuid.class != String or uuid.length < 27 then
72 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
74 new.private_reload(uuid)
77 ArvadosResourceList.new(self).where(*args)
80 ArvadosResourceList.new(self).limit(*args)
83 ArvadosResourceList.new(self).eager(*args)
86 ArvadosResourceList.new(self).all(*args)
90 self.class.columns.each do |col|
91 obdata[col.name.to_sym] = self.send(col.name.to_sym)
94 postdata = { self.class.to_s.underscore => obdata }
96 postdata['_method'] = 'PUT'
98 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
100 resp = $arvados_api_client.api(self.class, '', postdata)
102 return false if !resp[:etag] || !resp[:uuid]
104 # set read-only non-database attributes
108 # these attrs can be modified by "save" -- we should update our copies
109 %w(uuid owner created_at
110 modified_at modified_by_user modified_by_client
112 self.send(attr + '=', resp[attr.to_sym])
118 self.save or raise Exception.new("Save failed")
123 postdata = { '_method' => 'DELETE' }
124 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
125 resp[:etag] && resp[:uuid] && resp
133 o.merge!(args.pop) if args[-1].is_a? Hash
134 o[:link_class] ||= args.shift
135 o[:name] ||= args.shift
136 o[:head_kind] ||= args.shift
137 o[:tail_kind] = self.kind
138 o[:tail_uuid] = self.uuid
140 return all_links.select do |m|
145 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
153 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
154 @links = $arvados_api_client.unpack_api_response(@links)
157 return @all_links if @all_links
158 res = $arvados_api_client.api Link, '', {
161 tail_kind: self.kind,
166 @all_links = $arvados_api_client.unpack_api_response(res)
169 private_reload(self.uuid)
171 def private_reload(uuid_or_hash)
172 raise "No such object" if !uuid_or_hash
173 if uuid_or_hash.is_a? Hash
176 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
179 if self.respond_to?(k.to_s + '=')
180 self.send(k.to_s + '=', v)
182 # When ArvadosApiClient#schema starts telling us what to expect
183 # in API responses (not just the server side database
184 # columns), this sort of awfulness can be avoided:
185 self.instance_variable_set('@' + k.to_s, v)
186 if !self.respond_to? k
187 singleton = class << self; self end
188 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
199 def attributes_for_display
200 self.attributes.reject { |k,v|
201 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
203 attribute_sortkey[k] or k
213 (current_user.is_admin or
214 current_user.uuid == self.owner))
217 def attribute_editable?(attr)
218 if "created_at modified_at modified_by_user modified_by_client updated_at".index(attr.to_s)
220 elsif "uuid owner".index(attr.to_s)
221 current_user and current_user.is_admin
223 current_user and current_user.uuid == owner
227 def self.resource_class_for_uuid(uuid, opts={})
228 if uuid.is_a? ArvadosBase
231 unless uuid.is_a? String
234 if opts[:class].is_a? Class
237 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
241 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
242 resource_class ||= $arvados_api_client.
243 kind_class(self.uuid_infix_object_kind[re[1]])
245 if opts[:referring_object] and
246 opts[:referring_attr] and
247 opts[:referring_attr].match /_uuid$/
248 resource_class ||= $arvados_api_client.
249 kind_class(opts[:referring_object].
250 attributes[opts[:referring_attr].
251 sub(/_uuid$/, '_kind')])
264 def self.current_user
265 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
266 Thread.current[:user]
269 self.class.current_user