2760: Assign a generic name if link_class=name and no name is given.
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
index a3576bc83e153ad8f3daf62449b9cf9f240f787c..c79d082946504ed75ec90411520c94e5c7482d1f 100644 (file)
@@ -1,4 +1,6 @@
 class ApplicationController < ActionController::Base
+  include ArvadosApiClientHelper
+
   respond_to :html, :json, :js
   protect_from_forgery
 
@@ -64,29 +66,52 @@ class ApplicationController < ActionController::Base
     self.render_error status: 404
   end
 
+  def name_links_for object=nil
+    if !@name_links_cache or !@name_links_cache[object.uuid]
+      @name_links_cache ||= {}
+      uuids = @objects.collect(&:uuid) + [object.uuid] - @name_links_cache.keys
+      uuids.each do |uuid|
+        @name_links_cache[uuid] = []
+      end
+      offset = 0
+      while true
+        name_links = Link.
+          filter([['link_class', '=', 'name'],
+                  ['head_uuid', 'in', uuids]]).
+          offset(offset).
+          order(['uuid'])
+        name_links.each do |link|
+          @name_links_cache[link.head_uuid] << link
+        end
+        offset += name_links.result_limit
+        break if offset >= name_links.items_available
+      end
+    end
+    @name_links_cache[object.uuid] || []
+  end
+
   def index
+    @limit ||= 200
     if params[:limit]
-      limit = params[:limit].to_i
-    else
-      limit = 200
+      @limit = params[:limit].to_i
     end
 
+    @offset ||= 0
     if params[:offset]
-      offset = params[:offset].to_i
-    else
-      offset = 0
+      @offset = params[:offset].to_i
     end
 
+    @filters ||= []
     if params[:filters]
       filters = params[:filters]
       if filters.is_a? String
         filters = Oj.load filters
       end
-    else
-      filters = []
+      @filters += filters
     end
 
-    @objects ||= model_class.filter(filters).limit(limit).offset(offset).all
+    @objects ||= model_class
+    @objects = @objects.filter(@filters).limit(@limit).offset(@offset).all
     respond_to do |f|
       f.json { render json: @objects }
       f.html { render }
@@ -103,8 +128,12 @@ class ApplicationController < ActionController::Base
       f.html {
         if request.method == 'GET'
           render
+        elsif params[:return_to]
+          redirect_to params[:return_to]
+        elsif @name_link
+          redirect_to action: :show, id: @name_link.uuid
         else
-          redirect_to params[:return_to] || @object
+          redirect_to @object
         end
       }
       f.js { render }
@@ -149,6 +178,13 @@ class ApplicationController < ActionController::Base
     @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
     @object ||= model_class.new @new_resource_attrs
     @object.save!
+    if model_class != Link
+      @name_link = Link.new(tail_uuid: current_user.uuid,
+                            head_uuid: @object.uuid,
+                            link_class: 'name',
+                            name: params[:name])
+      @name_link.save!
+    end
     show
   end
 
@@ -199,7 +235,7 @@ class ApplicationController < ActionController::Base
     respond_to do |f|
       f.html {
         if request.method == 'GET'
-          redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
+          redirect_to arvados_api_client.arvados_login_url(return_to: request.url)
         else
           flash[:error] = "Either you are not logged in, or your session has timed out. I can't automatically log you in and re-attempt this request."
           redirect_to :back
@@ -253,6 +289,13 @@ class ApplicationController < ActionController::Base
     elsif params[:uuid].is_a? String
       if params[:uuid].empty?
         @object = nil
+      elsif model_class.to_s != 'Link' and
+          ArvadosBase::resource_class_for_uuid(params[:uuid]).to_s == 'Link'
+        @object = nil
+        if (@name_link = Link.where(uuid: params[:uuid],
+                                    link_class: 'name').first)
+          @object = model_class.where(uuid: @name_link.head_uuid).first
+        end
       else
         @object = model_class.find(params[:uuid])
       end