move queue and system user logic out of dispatch_jobs
[arvados.git] / services / api / app / controllers / application_controller.rb
index 3c4a8845e3ea10b3dc71d58d4197c2af44e03805..2f142b8a01936b03c3cd349bd3d4b2a49eafc74f 100644 (file)
@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
   before_filter :login_required, :except => :render_not_found
   before_filter :catch_redirect_hint
 
+  before_filter :load_where_param, :only => :index
   before_filter :find_objects_for_index, :only => :index
   before_filter :find_object_by_uuid, :except => [:index, :create]
 
@@ -88,6 +89,11 @@ class ApplicationController < ActionController::Base
 
   protected
 
+  def load_where_param
+    @where = params[:where] || {}
+    @where = Oj.load(@where) if @where.is_a?(String)
+  end
+
   def find_objects_for_index
     uuid_list = [current_user.uuid, *current_user.groups_i_can(:read)]
     sanitized_uuid_list = uuid_list.
@@ -98,9 +104,7 @@ class ApplicationController < ActionController::Base
             true, current_user.is_admin,
             uuid_list,
             current_user.uuid)
-    @where = params[:where] || {}
-    @where = Oj.load(@where) if @where.is_a?(String)
-    if params[:where]
+    if !@where.empty?
       conditions = ['1=1']
       @where.each do |attr,value|
         if attr == 'any'
@@ -147,7 +151,22 @@ class ApplicationController < ActionController::Base
     else
       @objects = @objects.limit(100)
     end
-    @objects = @objects.order("#{table_name}.modified_at desc")
+    orders = []
+    if params[:order]
+      params[:order].split(',').each do |order|
+        attr, direction = order.strip.split " "
+        direction ||= 'asc'
+        if attr.match /^[a-z][_a-z0-9]+$/ and
+            model_class.columns.collect(&:name).index(attr) and
+            ['asc','desc'].index direction.downcase
+          orders << "#{table_name}.#{attr} #{direction.downcase}"
+        end
+      end
+    end
+    if orders.empty?
+      orders << "#{table_name}.modified_at desc"
+    end
+    @objects = @objects.order(orders.join ", ")
   end
 
   def resource_attrs