2760: Assign a generic name if link_class=name and no name is given.
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
index f9de62d60e3d3ebe4613f4f29c97726e28d60549..c79d082946504ed75ec90411520c94e5c7482d1f 100644 (file)
@@ -1,4 +1,6 @@
 class ApplicationController < ActionController::Base
+  include ArvadosApiClientHelper
+
   respond_to :html, :json, :js
   protect_from_forgery
 
@@ -12,6 +14,7 @@ class ApplicationController < ActionController::Base
   before_filter :check_user_notifications, except: ERROR_ACTIONS
   around_filter :using_reader_tokens, only: [:index, :show]
   before_filter :find_object_by_uuid, except: [:index] + ERROR_ACTIONS
+  before_filter :check_my_folders, :except => ERROR_ACTIONS
   theme :select_theme
 
   begin
@@ -63,20 +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
 
-    @objects ||= model_class.limit(limit).offset(offset).all
+    @filters ||= []
+    if params[:filters]
+      filters = params[:filters]
+      if filters.is_a? String
+        filters = Oj.load filters
+      end
+      @filters += filters
+    end
+
+    @objects ||= model_class
+    @objects = @objects.filter(@filters).limit(@limit).offset(@offset).all
     respond_to do |f|
       f.json { render json: @objects }
       f.html { render }
@@ -89,12 +124,16 @@ class ApplicationController < ActionController::Base
       return render_not_found("object not found")
     end
     respond_to do |f|
-      f.json { render json: @object }
+      f.json { render json: @object.attributes.merge(href: url_for(@object)) }
       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 }
@@ -134,16 +173,19 @@ class ApplicationController < ActionController::Base
   end
 
   def create
-    @object ||= model_class.new params[model_class.to_s.underscore.singularize]
+    @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
+    @new_resource_attrs ||= {}
+    @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
+    @object ||= model_class.new @new_resource_attrs
     @object.save!
-
-    respond_to do |f|
-      f.json { render json: @object }
-      f.html {
-        redirect_to(params[:return_to] || @object)
-      }
-      f.js { render }
+    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
 
   def destroy
@@ -193,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
@@ -242,8 +284,21 @@ class ApplicationController < ActionController::Base
     if params[:id] and params[:id].match /\D/
       params[:uuid] = params.delete :id
     end
-    if params[:uuid].is_a? String
-      @object = model_class.find(params[:uuid])
+    if not model_class
+      @object = nil
+    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
     else
       @object = model_class.where(uuid: params[:uuid]).first
     end
@@ -410,6 +465,15 @@ class ApplicationController < ActionController::Base
     }
   }
 
+  def check_my_folders
+    @my_top_level_folders = lambda do
+      @top_level_folders ||= Group.
+        filter([['group_class','=','folder'],
+                ['owner_uuid','=',current_user.uuid]]).
+        sort_by { |x| x.name || '' }
+    end
+  end
+
   def check_user_notifications
     @notification_count = 0
     @notifications = []