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 ||= {
28 'owner_uuid' => '001',
29 'created_at' => '002',
30 'modified_at' => '003',
31 'modified_by_user_uuid' => '004',
32 'modified_by_client_uuid' => '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).order(*args)
80 ArvadosResourceList.new(self).where(*args)
83 ArvadosResourceList.new(self).limit(*args)
86 ArvadosResourceList.new(self).eager(*args)
89 ArvadosResourceList.new(self).all(*args)
93 self.class.columns.each do |col|
94 obdata[col.name.to_sym] = self.send(col.name.to_sym)
97 postdata = { self.class.to_s.underscore => obdata }
99 postdata['_method'] = 'PUT'
101 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
103 resp = $arvados_api_client.api(self.class, '', postdata)
105 return false if !resp[:etag] || !resp[:uuid]
107 # set read-only non-database attributes
111 # these attrs can be modified by "save" -- we should update our copies
112 %w(uuid owner_uuid created_at
113 modified_at modified_by_user_uuid modified_by_client_uuid
115 if self.respond_to? "#{attr}=".to_sym
116 self.send(attr + '=', resp[attr.to_sym])
123 self.save or raise Exception.new("Save failed")
128 postdata = { '_method' => 'DELETE' }
129 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
130 resp[:etag] && resp[:uuid] && resp
138 o.merge!(args.pop) if args[-1].is_a? Hash
139 o[:link_class] ||= args.shift
140 o[:name] ||= args.shift
141 o[:head_kind] ||= args.shift
142 o[:tail_kind] = self.kind
143 o[:tail_uuid] = self.uuid
145 return all_links.select do |m|
150 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
158 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
159 @links = $arvados_api_client.unpack_api_response(@links)
162 return @all_links if @all_links
163 res = $arvados_api_client.api Link, '', {
166 tail_kind: self.kind,
171 @all_links = $arvados_api_client.unpack_api_response(res)
174 private_reload(self.uuid)
176 def private_reload(uuid_or_hash)
177 raise "No such object" if !uuid_or_hash
178 if uuid_or_hash.is_a? Hash
181 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
184 if self.respond_to?(k.to_s + '=')
185 self.send(k.to_s + '=', v)
187 # When ArvadosApiClient#schema starts telling us what to expect
188 # in API responses (not just the server side database
189 # columns), this sort of awfulness can be avoided:
190 self.instance_variable_set('@' + k.to_s, v)
191 if !self.respond_to? k
192 singleton = class << self; self end
193 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
204 def attributes_for_display
205 self.attributes.reject { |k,v|
206 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
208 attribute_sortkey[k] or k
217 (current_user and current_user.is_active and
218 (current_user.is_admin or
219 current_user.uuid == self.owner_uuid))
222 def attribute_editable?(attr)
223 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
225 elsif not (current_user.andand.is_active)
227 elsif "uuid owner_uuid".index(attr.to_s) or current_user.is_admin
228 current_user.is_admin
230 current_user.uuid == self.owner_uuid or current_user.uuid == self.uuid
234 def self.resource_class_for_uuid(uuid, opts={})
235 if uuid.is_a? ArvadosBase
238 unless uuid.is_a? String
241 if opts[:class].is_a? Class
244 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
248 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
249 resource_class ||= $arvados_api_client.
250 kind_class(self.uuid_infix_object_kind[re[1]])
252 if opts[:referring_object] and
253 opts[:referring_attr] and
254 opts[:referring_attr].match /_uuid$/
255 resource_class ||= $arvados_api_client.
256 kind_class(opts[:referring_object].
257 attributes[opts[:referring_attr].
258 sub(/_uuid$/, '_kind')])
263 def friendly_link_name
264 if self.class.column_names.include? 'name'
277 def self.current_user
278 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
279 Thread.current[:user]
282 self.class.current_user