2760: Make editable? follow permissions obtained via groups/folders.
[arvados.git] / apps / workbench / app / models / arvados_resource_list.rb
index 1e2e720d4e8a4f897ddca8c0b3a35f18766f7740..dedd18c81d7eb21193523a52c2dde99cee3176cf 100644 (file)
@@ -2,7 +2,7 @@ class ArvadosResourceList
   include ArvadosApiClientHelper
   include Enumerable
 
-  def initialize(resource_class)
+  def initialize resource_class=nil
     @resource_class = resource_class
   end
 
@@ -91,6 +91,12 @@ class ArvadosResourceList
     self
   end
 
+  def collect
+    results.collect do |m|
+      yield m
+    end
+  end
+
   def first
     results.first
   end
@@ -135,4 +141,38 @@ class ArvadosResourceList
     results.offset if results.respond_to? :offset
   end
 
+  def result_links
+    results.links if results.respond_to? :links
+  end
+
+  # Return links provided with API response that point to the
+  # specified object, and have the specified link_class. If link_class
+  # is false or omitted, return all links pointing to the specified
+  # object.
+  def links_for item_or_uuid, link_class=false
+    return [] if !result_links
+    unless @links_for_uuid
+      @links_for_uuid = {}
+      result_links.each do |link|
+        if link.respond_to? :head_uuid
+          @links_for_uuid[link.head_uuid] ||= []
+          @links_for_uuid[link.head_uuid] << link
+        end
+      end
+    end
+    if item_or_uuid.respond_to? :uuid
+      uuid = item_or_uuid.uuid
+    else
+      uuid = item_or_uuid
+    end
+    (@links_for_uuid[uuid] || []).select do |link|
+      link_class == false or link.link_class == link_class
+    end
+  end
+
+  # Note: this arbitrarily chooses one of (possibly) multiple names.
+  def name_for item_or_uuid
+    links_for(item_or_uuid, 'name').first.andand.name
+  end
+
 end