support order param
authorTom Clegg <tom@clinicalfuture.com>
Wed, 22 May 2013 18:49:52 +0000 (11:49 -0700)
committerTom Clegg <tom@clinicalfuture.com>
Wed, 22 May 2013 18:49:52 +0000 (11:49 -0700)
services/api/app/controllers/application_controller.rb

index 3c4a8845e3ea10b3dc71d58d4197c2af44e03805..fca8809ee63d220597ea8ca57cdabd2697c90cc9 100644 (file)
@@ -147,7 +147,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