Merge branch 'master' into 2871-preload-objects
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
index e1b6a7f929450e8320941ee9b9fe030f791b47ae..786479cc0ded746047a92dd06d27c1e21559f21d 100644 (file)
@@ -191,6 +191,31 @@ class ApplicationController < ActionController::Base
     %w(Attributes Metadata JSON API)
   end
 
+  # helper method to get links for given objects or uuids
+  helper_method :links_for_object
+  def links_for_object object_or_uuid
+    uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
+    preload_links_for_objects([uuid])
+    @all_links_for[uuid]
+  end
+
+  helper_method :preload_links_for_objects
+  def preload_links_for_objects objects_and_uuids
+    uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
+    @all_links_for ||= {}
+    if not uuids.select { |x| @all_links_for[x].nil? }.any?
+      # already preloaded for all of these uuids
+      return
+    end
+    uuids.each do |x|
+      @all_links_for[x] = []
+    end
+    # TODO: make sure we get every page of results from API server
+    Link.filter([['head_uuid','in',uuids]]).each do |link|
+      @all_links_for[link.head_uuid] << link
+    end
+  end
+
   protected
 
   def redirect_to_login