From ba78b934a61116b99d38422fb501f8b677b1d4d7 Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
Date: Tue, 7 Feb 2017 11:53:45 -0200
Subject: [PATCH] Filtered empty collections from index action

Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
---
 app/controllers/v1/collections_controller.rb |  2 +-
 app/policies/collection_policy.rb            | 10 ++--------
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/app/controllers/v1/collections_controller.rb b/app/controllers/v1/collections_controller.rb
index b9088999..a46d1438 100644
--- a/app/controllers/v1/collections_controller.rb
+++ b/app/controllers/v1/collections_controller.rb
@@ -16,7 +16,7 @@ class V1::CollectionsController < ApplicationController
   # GET /v1/collections
   # GET /v1/collections.json
   def index
-    collections = paginate Collection
+    collections = paginate policy_scope(Collection)
     render json: collections
   end
 
diff --git a/app/policies/collection_policy.rb b/app/policies/collection_policy.rb
index 8aeafcd2..7ca595c7 100644
--- a/app/policies/collection_policy.rb
+++ b/app/policies/collection_policy.rb
@@ -6,17 +6,11 @@ class CollectionPolicy < ApplicationPolicy
   include StageablePolicy
 
   class Scope < Scope
-    def initialize(user, user_id, scope)
-      @user = user
-      @user_id = user_id
-      @scope = scope
-    end
-
     def resolve
-      if @user.try(:id) == @user_id || @user.try(:is_admin?)
+      if !user.nil? && user.is_admin?
         scope.all
       else
-        scope.where(privacy: 'public')
+        scope.includes(:collection_items).where.not(:collection_items => {:collection_id => nil}).where(privacy: 'public')
       end
     end
   end
-- 
GitLab