From 6e5dd4974c9f2dff84a3dca8e29f587fa4b16f34 Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
Date: Mon, 5 Dec 2016 09:50:02 -0200
Subject: [PATCH] Created ActivitiesFilterService to maintain the code in one
 place

Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
---
 app/models/concerns/trackable.rb          | 22 +--------------------
 app/models/user.rb                        | 24 +----------------------
 app/policies/activity_policy.rb           | 18 ++---------------
 app/services/activities_filter_service.rb | 22 +++++++++++++++++++++
 4 files changed, 26 insertions(+), 60 deletions(-)
 create mode 100644 app/services/activities_filter_service.rb

diff --git a/app/models/concerns/trackable.rb b/app/models/concerns/trackable.rb
index 4a6712ced..a8da93f4f 100644
--- a/app/models/concerns/trackable.rb
+++ b/app/models/concerns/trackable.rb
@@ -1,6 +1,7 @@
 module Trackable
   extend ActiveSupport::Concern
   include PublicActivity::Model
+  include ActivitiesFilterService
 
   included do
     tracked owner: proc { |controller, model| model.try(:user) || model.try(:owner) || controller.try(:current_user) }
@@ -8,25 +9,4 @@ module Trackable
     tracked privacy: proc { |_controller, model| model.try(:privacy) || "public" }
 
   end
-
-  def activities_filtered
-    p "--------------------- LOLOLOLOLLOLO"
-    @activities.where.not(key: activities_filter)
-  end
-
-  def activities_filter
-    [ 'complaint.update', 'complaint.destroy',
-      'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
-      'institution.create', 'institution.update', 'institution.destroy',
-      'review.update', 'review.destroy',
-      'rate.create', 'rate.update', 'rate.destroy',
-      'follow.create', 'follow.update', 'follow.destroy',
-      'share.create', 'share.update', 'share.destroy',
-      'view.create', 'view.update', 'view.destroy',
-      'tagging.create', 'tagging.update', 'tagging.destroy',
-      'bookmark.create', 'bookmark.update', 'bookmark.destroy',
-      'download.update', 'download.destroy',
-      'like.update'
-    ]
-  end
 end
diff --git a/app/models/user.rb b/app/models/user.rb
index b71382e63..03aa1154b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -41,6 +41,7 @@ class User < ApplicationRecord
   include Complainable
   include Publisher
   include PublicActivity::Common
+  include ActivitiesFilterService
 
   after_create -> { new_activity("create") }
   after_update -> { new_activity("update") }
@@ -177,29 +178,6 @@ class User < ApplicationRecord
 
   # ~~~~ end followable actions ~~~~ #
 
-  def activities_filtered
-    p "--------------------- LOLOLOLOLLOLO"
-    self.activities.where.not(key: activities_filter)
-  end
-
-  private
-
-  def activities_filter
-    [ 'complaint.update', 'complaint.destroy',
-      'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
-      'institution.create', 'institution.update', 'institution.destroy',
-      'review.update', 'review.destroy',
-      'rate.create', 'rate.update', 'rate.destroy',
-      'follow.create', 'follow.update', 'follow.destroy',
-      'share.create', 'share.update', 'share.destroy',
-      'view.create', 'view.update', 'view.destroy',
-      'tagging.create', 'tagging.update', 'tagging.destroy',
-      'bookmark.create', 'bookmark.update', 'bookmark.destroy',
-      'download.update', 'download.destroy',
-      'like.update'
-    ]
-  end
-
   def default_role
     roles << Role.teacher
   end
diff --git a/app/policies/activity_policy.rb b/app/policies/activity_policy.rb
index 77abe14d5..ee15995f6 100644
--- a/app/policies/activity_policy.rb
+++ b/app/policies/activity_policy.rb
@@ -1,5 +1,6 @@
 class ActivityPolicy < ApplicationPolicy
   class Scope < Scope
+    include ActivitiesFilterService
     attr_reader :user, :scope
 
     def initialize(user, scope)
@@ -8,24 +9,9 @@ class ActivityPolicy < ApplicationPolicy
     end
 
     def resolve
-      scope.includes(:owner,:recipient).where("privacy = 'public'").where.not(key: except).order('created_at DESC').all
+      scope.includes(:owner,:recipient).where("privacy = 'public'").where.not(key: activities_filter).order('created_at DESC').all
     end
 
-    def except
-      [ 'complaint.update', 'complaint.destroy',
-        'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
-        'institution.create', 'institution.update', 'institution.destroy',
-        'review.update', 'review.destroy',
-        'rate.create', 'rate.update', 'rate.destroy',
-        'follow.create', 'follow.update', 'follow.destroy',
-        'share.create', 'share.update', 'share.destroy',
-        'view.create', 'view.update', 'view.destroy',
-        'tagging.create', 'tagging.update', 'tagging.destroy',
-        'bookmark.create', 'bookmark.update', 'bookmark.destroy',
-        'download.update', 'download.destroy',
-        'like.update'
-      ]
-    end
   end
 
   def index?
diff --git a/app/services/activities_filter_service.rb b/app/services/activities_filter_service.rb
new file mode 100644
index 000000000..c4e763ff1
--- /dev/null
+++ b/app/services/activities_filter_service.rb
@@ -0,0 +1,22 @@
+module ActivitiesFilterService
+
+  def activities_filtered
+    self.activities.where.not(key: activities_filter)
+  end
+
+  def activities_filter
+    [ 'complaint.update', 'complaint.destroy',
+      'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
+      'institution.create', 'institution.update', 'institution.destroy',
+      'review.update', 'review.destroy',
+      'rate.create', 'rate.update', 'rate.destroy',
+      'follow.create', 'follow.update', 'follow.destroy',
+      'share.create', 'share.update', 'share.destroy',
+      'view.create', 'view.update', 'view.destroy',
+      'tagging.create', 'tagging.update', 'tagging.destroy',
+      'bookmark.create', 'bookmark.update', 'bookmark.destroy',
+      'download.update', 'download.destroy',
+      'like.update'
+    ]
+  end
+end
-- 
GitLab