[ARVADOS] created: fe78b629dc39298c5990a093e73b7c6231f387b0

git at public.curoverse.com git at public.curoverse.com
Tue Apr 29 16:14:28 EDT 2014


        at  fe78b629dc39298c5990a093e73b7c6231f387b0 (commit)


commit fe78b629dc39298c5990a093e73b7c6231f387b0
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Tue Apr 29 16:14:23 2014 -0400

    Added support for :distinct to application controller.  User can now specify a
    column and only get back rows with distinct values of that column.

diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb
index a078ae5..4eb0d8e 100644
--- a/services/api/app/controllers/application_controller.rb
+++ b/services/api/app/controllers/application_controller.rb
@@ -261,6 +261,11 @@ class ApplicationController < ActionController::Base
     end
 
     @select = params[:select] if params[:select].andand.is_a? Array
+
+    if params[:distinct].is_a? String
+      @distinct = params[:distinct]
+      @orders.select! { |o| @select.include? o } if @select
+    end
   end
 
   def find_objects_for_index
@@ -377,6 +382,7 @@ class ApplicationController < ActionController::Base
     end
 
     @objects = @objects.select(@select.map { |s| "#{table_name}.#{ActiveRecord::Base.connection.quote_column_name s.to_s}" }.join ", ") if @select
+    @objects = @objects.uniq(ActiveRecord::Base.connection.quote_column_name @distinct.to_s) if @distinct
     @objects = @objects.order(@orders.join ", ") if @orders.any?
     @objects = @objects.limit(@limit)
     @objects = @objects.offset(@offset)
diff --git a/services/api/test/integration/select_test.rb b/services/api/test/integration/select_test.rb
index 38b1b95..522daa7 100644
--- a/services/api/test/integration/select_test.rb
+++ b/services/api/test/integration/select_test.rb
@@ -9,4 +9,10 @@ class SelectTest < ActionDispatch::IntegrationTest
     }.count
   end
 
+  test "should only get distinct values" do
+    get "/arvados/v1/links", {:format => :json, :select => ['link_class'], :distinct => "link_class"}, auth(:active)
+    assert_response :success
+    assert_equal json_response['items'].uniq.count, json_response['items'].count
+  end
+
 end

commit 7a73008bcd015300461c34ce852d660f8244e11f
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Tue Apr 29 15:35:23 2014 -0400

    Added ability to select which fields to return, instead of always returning all fields.

diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb
index b091397..a078ae5 100644
--- a/services/api/app/controllers/application_controller.rb
+++ b/services/api/app/controllers/application_controller.rb
@@ -1,7 +1,21 @@
+module ApiTemplateOverride
+  def allowed_to_render?(fieldset, field, model, options)
+    if options[:select]
+      return options[:select].include? field.to_s
+    end
+    super
+  end
+end
+
+class ActsAsApi::ApiTemplate
+  prepend ApiTemplateOverride
+end
+
 class ApplicationController < ActionController::Base
   include CurrentApiClient
   include ThemesForRails::ActionController
 
+
   respond_to :json
   protect_from_forgery
   around_filter :thread_with_auth_info, :except => [:render_error, :render_not_found]
@@ -29,7 +43,7 @@ class ApplicationController < ActionController::Base
   DEFAULT_LIMIT = 100
 
   def index
-    @objects.uniq!(&:id)
+    @objects.uniq!(&:id) if @select.nil? or @select.include? "id"
     if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
       @objects.each(&:eager_load_associations)
     end
@@ -245,6 +259,8 @@ class ApplicationController < ActionController::Base
     if @orders.empty?
       @orders = default_orders
     end
+
+    @select = params[:select] if params[:select].andand.is_a? Array
   end
 
   def find_objects_for_index
@@ -360,6 +376,7 @@ class ApplicationController < ActionController::Base
       end
     end
 
+    @objects = @objects.select(@select.map { |s| "#{table_name}.#{ActiveRecord::Base.connection.quote_column_name s.to_s}" }.join ", ") if @select
     @objects = @objects.order(@orders.join ", ") if @orders.any?
     @objects = @objects.limit(@limit)
     @objects = @objects.offset(@offset)
@@ -536,7 +553,7 @@ class ApplicationController < ActionController::Base
       :self_link => "",
       :offset => @offset,
       :limit => @limit,
-      :items => @objects.as_api_response(nil)
+      :items => @objects.as_api_response(nil, {select: @select})
     }
     if @objects.respond_to? :except
       @object_list[:items_available] = @objects.
diff --git a/services/api/test/integration/select_test.rb b/services/api/test/integration/select_test.rb
new file mode 100644
index 0000000..38b1b95
--- /dev/null
+++ b/services/api/test/integration/select_test.rb
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class SelectTest < ActionDispatch::IntegrationTest
+  test "should select just two columns" do
+    get "/arvados/v1/links", {:format => :json, :select => ['uuid', 'link_class']}, auth(:active)
+    assert_response :success
+    assert_equal json_response['items'].count, json_response['items'].select { |i|
+      i['uuid'] != nil and i['link_class'] != nil and i['head_uuid'] == nil and i['tail_uuid'] == nil
+    }.count
+  end
+
+end

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list