implement general params[:where] and eager loading of associations
[arvados.git] / app / controllers / application_controller.rb
index 6ea18ce315e3acbc0fd02e274e8ae81ba11df9c4..2702130ee5f62be6087c9e62328ba99f89e53f82 100644 (file)
@@ -33,7 +33,24 @@ class ApplicationController < ActionController::Base
   end
 
   def index
+    @objects ||= if params[:where]
+      where = params[:where]
+      where = JSON.parse(where) if where.is_a?(String)
+      conditions = ['1=1']
+      where.each do |attr,value|
+        if (!value.nil? and
+            attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
+            model_class.columns.collect(&:name).index(attr))
+          conditions[0] << " and #{attr}=?"
+          conditions << value
+        end
+      end
+      model_class.where(*conditions)
+    end
     @objects ||= model_class.all
+    if params[:eager]
+      @objects.each(&:eager_load_associations)
+    end
     render_list
   end