Merge branch 'master' into 4904-arv-web
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
index 6a2b92d7669cfcacec80a99f652969e8d98cf0a3..c82ffb49cd7af8e4b7fe2afac128474718de71c8 100644 (file)
@@ -6,6 +6,8 @@ class Arvados::V1::GroupsController < ApplicationController
               uuid: {
                 type: 'string', required: false, default: nil
               },
+              # include_linked returns name links, which are obsolete, so
+              # remove it when clients have been migrated.
               include_linked: {
                 type: 'boolean', required: false, default: false
               },
@@ -34,6 +36,8 @@ class Arvados::V1::GroupsController < ApplicationController
 
   def contents
     # Set @objects:
+    # include_linked returns name links, which are obsolete, so
+    # remove it when clients have been migrated.
     load_searchable_objects(owner_uuid: @object.andand.uuid,
                             include_linked: params[:include_linked])
     sql = 'link_class=? and head_uuid in (?)'
@@ -53,7 +57,7 @@ class Arvados::V1::GroupsController < ApplicationController
       :items_available => @items_available,
       :items => @objects.as_api_response(nil)
     }
-    render json: @object_list
+    send_json @object_list
   end
 
   protected
@@ -67,6 +71,10 @@ class Arvados::V1::GroupsController < ApplicationController
     # aggregate set.
     limit_all = @limit
     offset_all = @offset
+    # save the orders from the current request as determined by load_param,
+    # but otherwise discard them because we're going to be getting objects
+    # from many models
+    request_orders = @orders.clone
     @orders = []
 
     [Group,
@@ -82,19 +90,21 @@ class Arvados::V1::GroupsController < ApplicationController
         cond_params = []
         conds << "#{klass.table_name}.owner_uuid = ?"
         cond_params << opts[:owner_uuid]
-        if opts[:include_linked]
-          haslink = "#{klass.table_name}.uuid IN (SELECT head_uuid FROM links WHERE link_class=#{klass.sanitize 'name'}"
-          haslink += " AND links.tail_uuid=#{klass.sanitize opts[:owner_uuid]}"
-          haslink += ")"
-          conds << haslink
-        end
         if conds.any?
           cond_sql = '(' + conds.join(') OR (') + ')'
           @objects = @objects.where(cond_sql, *cond_params)
         end
       end
 
-      @objects = @objects.order("#{klass.table_name}.uuid")
+      # If the currently requested orders specifically match the table_name for the current klass, apply the order
+      request_order = request_orders && request_orders.find{ |r| r =~ /^#{klass.table_name}\./i }
+      if request_order
+        @objects = @objects.order(request_order)
+      else
+        # default to created_at desc, ignoring any currently requested ordering because it doesn't apply to this klass
+        @objects = @objects.order("#{klass.table_name}.created_at desc")
+      end
+
       @limit = limit_all - all_objects.count
       apply_where_limit_order_params klass
       klass_items_available = @objects.