From a500d074c3e1674a4c0a7a70c963ff6a726d82cf Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
Date: Wed, 30 Mar 2016 11:40:34 -0300
Subject: [PATCH] Preventing user from duplicating learning object on
 collection (Fix #37)

Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
---
 app/controllers/collections_controller.rb | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb
index 34fb0690..60173d6e 100644
--- a/app/controllers/collections_controller.rb
+++ b/app/controllers/collections_controller.rb
@@ -96,7 +96,9 @@ class CollectionsController < ApplicationController
     @collection = nil if @collection == 'all'
 
     @collections = current_user.associated_collections
-    @collections.select! { |c| c.id != @collection.id } unless @collection.blank?
+    @collections.select! do |c|
+      (!@collection.blank? && c.id != @collection.id) || !include_learning_objects?(c)
+    end
     @type = params[:type]
 
     unless @type.blank?
@@ -138,7 +140,7 @@ class CollectionsController < ApplicationController
       next unless collection.user_own?(current_user)
 
       @learning_objects.each do |learning_object|
-        collection.learning_objects << learning_object
+        collection.learning_objects << learning_object unless collection.learning_objects.include? learning_object
       end
       collection.save
     end
@@ -171,6 +173,13 @@ class CollectionsController < ApplicationController
 
   private
 
+  def include_learning_objects?(collection)
+    @learning_objects.each do |lo|
+      return true if collection.learning_objects.include? lo
+    end
+    false
+  end
+
   def check_collection_privacy!(collection)
     if collection.private? && !collection.user_own?(current_user)
       redirect_to :root, notice: 'Está é uma coleção privada.'
-- 
GitLab