From 09954368d70ca292e73336fbad1b55d6711992a9 Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Tue, 27 Oct 2015 11:38:35 -0200
Subject: [PATCH] add method for find collections of a learning object and
 rendering json

---
 app/controllers/learning_objects_controller.rb      |  7 ++++++-
 app/models/learning_object.rb                       |  4 ++++
 app/repositories/orient_db/collection_repository.rb | 11 ++++++++++-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb
index 2b4dc78f..ac12721c 100644
--- a/app/controllers/learning_objects_controller.rb
+++ b/app/controllers/learning_objects_controller.rb
@@ -1,5 +1,5 @@
 class LearningObjectsController < ApplicationController
-  before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like, :bookmark]
+  before_action :set_learning_object, only: [:show, :edit, :update, :destroy, :like, :bookmarks, :collections]
   after_action :increment_learning_object_views, only: [:show]
   before_action :authenticate_user!, except: [:index, :show, :like]
   before_action :set_complaint_messages, only: :show
@@ -88,6 +88,11 @@ class LearningObjectsController < ApplicationController
     end
   end
 
+  # GET /learning_objects/1/collections.json
+  def collections
+    @collections = @learning_object.collections
+  end
+
   private
 
   # Use callbacks to share common setup or constraints between actions.
diff --git a/app/models/learning_object.rb b/app/models/learning_object.rb
index 5f9b4f8c..aa2935bd 100644
--- a/app/models/learning_object.rb
+++ b/app/models/learning_object.rb
@@ -16,6 +16,10 @@ class LearningObject
     super(params.merge(defaults))
   end
 
+  def collections
+    collection_repository.find_by_learning_object self
+  end
+
   def like(user)
     learning_object_repository.like user, self
   end
diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb
index 00fd055e..8b8be44f 100644
--- a/app/repositories/orient_db/collection_repository.rb
+++ b/app/repositories/orient_db/collection_repository.rb
@@ -43,7 +43,16 @@ module OrientDb
 
     def all(user)
       query = sprintf("select * from (select expand(in('BelongsTo')) from %s) where name<>'Bookmarks'", user.rid)
-      objects = build_objects connection.query(query)
+      build_objects connection.query(query)
+    end
+
+    def find_by_learning_object(learning_object)
+      if learning_object.id.to_s.empty?
+        return []
+      end
+
+      query = sprintf("select * from %s where learning_objects contains %s", odb_class, learning_object.id)
+      build_objects connection.query(query)
     end
 
     def bookmarks(user)
-- 
GitLab