show cli update example on generic object#get page
[arvados.git] / apps / workbench / app / models / arvados_base.rb
index e6d10f0eeb7cba2fe03cfa980684bcb11872aa35..267a4a9133ef1c9b5c05372488faa349d0ae332e 100644 (file)
@@ -43,6 +43,7 @@ class ArvadosBase < ActiveRecord::Base
   def self.columns
     return @columns unless @columns.nil?
     @columns = []
+    @attribute_info ||= {}
     return @columns if $arvados_api_client.arvados_schema[self.to_s.to_sym].nil?
     $arvados_api_client.arvados_schema[self.to_s.to_sym].each do |coldef|
       k = coldef[:name].to_sym
@@ -53,6 +54,7 @@ class ArvadosBase < ActiveRecord::Base
         serialize k, coldef[:type].constantize
       end
       attr_accessible k
+      @attribute_info[k] = coldef
     end
     attr_reader :etag
     attr_reader :kind
@@ -61,6 +63,10 @@ class ArvadosBase < ActiveRecord::Base
   def self.column(name, sql_type = nil, default = nil, null = true)
     ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
   end
+  def self.attribute_info
+    self.columns
+    @attribute_info
+  end
   def self.find(uuid)
     if uuid.class != String or uuid.length < 27 then
       raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
@@ -111,6 +117,17 @@ class ArvadosBase < ActiveRecord::Base
   def save!
     self.save or raise Exception.new("Save failed")
   end
+
+  def destroy
+    if etag || uuid
+      postdata = { '_method' => 'DELETE' }
+      resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
+      resp[:etag] && resp[:uuid] && resp
+    else
+      true
+    end
+  end
+      
   def links(*args)
     o = {}
     o.merge!(args.pop) if args[-1].is_a? Hash
@@ -187,6 +204,26 @@ class ArvadosBase < ActiveRecord::Base
     }
   end
 
+  def self.creatable?
+    current_user
+  end
+
+  def editable?
+    (current_user and
+     (current_user.is_admin or
+      current_user.uuid == self.owner))
+  end
+
+  def attribute_editable?(attr)
+    if "created_at modified_at modified_by_user modified_by_client updated_at".index(attr.to_s)
+      false
+    elsif "uuid owner".index(attr.to_s)
+      current_user and current_user.is_admin
+    else
+      current_user and current_user.uuid == owner
+    end
+  end
+
   def self.resource_class_for_uuid(uuid, opts={})
     if uuid.is_a? ArvadosBase
       return uuid.class
@@ -223,4 +260,12 @@ class ArvadosBase < ActiveRecord::Base
     @etag = nil
     self
   end
+
+  def self.current_user
+    Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
+    Thread.current[:user]
+  end
+  def current_user
+    self.class.current_user
+  end
 end