Merge branch '2189-log-in-as-user'
[arvados.git] / apps / workbench / app / models / arvados_base.rb
index 5c7e44f136b3417a8a4f9fa4db1a0d5bf1142b35..72b76a522982d8e04b595b4ecda9dc0a01523504 100644 (file)
@@ -68,11 +68,21 @@ class ArvadosBase < ActiveRecord::Base
     self.columns
     @attribute_info
   end
-  def self.find(uuid)
+  def self.find(uuid, opts={})
     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'
     end
-    new.private_reload(uuid)
+
+    # Only do one lookup on the API side per {class, uuid, workbench
+    # request} unless {cache: false} is given via opts.
+    cache_key = "request_#{Thread.current.object_id}_#{self.to_s}_#{uuid}"
+    if opts[:cache] == false
+      Rails.cache.write cache_key, $arvados_api_client.api(self, '/' + uuid)
+    end
+    hash = Rails.cache.fetch cache_key do
+      $arvados_api_client.api(self, '/' + uuid)
+    end
+    new.private_reload(hash)
   end
   def self.order(*args)
     ArvadosResourceList.new(self).order(*args)
@@ -262,9 +272,7 @@ class ArvadosBase < ActiveRecord::Base
   end
 
   def friendly_link_name
-    if self.class.column_names.include? 'name'
-      self.name
-    end
+    (name if self.respond_to? :name) || uuid
   end
 
   protected