From e8fd2af5d0615a47f8d5ddbb63a9864b03ed3e6d Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Mon, 19 Oct 2015 10:35:26 -0200
Subject: [PATCH] adding new verifications for bookmarks and has_bookmarks?
 methods

---
 app/models/collection.rb                      |  2 +-
 .../orient_db/collection_repository.rb        | 22 ++++++++++++++-----
 lib/repository/environments.rb                |  1 -
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/app/models/collection.rb b/app/models/collection.rb
index 07142afcd..ba92a996d 100644
--- a/app/models/collection.rb
+++ b/app/models/collection.rb
@@ -6,7 +6,7 @@ class Collection
   validates_presence_of :name, :created_at, :owner, :learning_objects
   validates_with Validators::CollectionOwnerValidator
 
-  def initialize(params={})
+  def initialize(params = {})
     super(params.merge(defaults))
   end
 
diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb
index 8abe39d56..b560ea67e 100644
--- a/app/repositories/orient_db/collection_repository.rb
+++ b/app/repositories/orient_db/collection_repository.rb
@@ -3,8 +3,7 @@ module OrientDb
     include OrientDb::Methods::EdgeMethods
 
     def build_object(args={})
-      Collection.new(args.merge(id: args['@rid'],
-                                name: args['name']))
+      Collection.new(args)
     end
 
     def create_bookmarks_collection(user)
@@ -16,9 +15,8 @@ module OrientDb
     end
 
     def destroy_bookmarks_collection(user)
-      bookmarks = Bookmarks.new(owner: user)
-
-      if has_bookmarks?(user)
+      bookmarks = user.bookmarks
+      if has_bookmarks? bookmarks
         destroy bookmarks
       end
     end
@@ -39,10 +37,22 @@ module OrientDb
 
     def bookmarks(user)
       result = connection.query(sprintf("select expand(in('BelongsTo')) from %s where name = '%s'", user.rid, 'Bookmarks'))
-      build_object(result.first)
+
+      if !result.empty?
+        return build_object(result.first)
+      end
+
+      # returns a null object
+      Bookmarks.new(owner: user)
     end
 
     def has_bookmarks?(user)
+      # checks if bookmarks id is defined
+      if user.bookmarks.id.nil?
+        return false
+      end
+
+      # checks if edge exists
       edge_exists? 'BelongsTo', user.bookmarks.id, user.rid
     end
 
diff --git a/lib/repository/environments.rb b/lib/repository/environments.rb
index 55ea9ae9a..4b6d90d31 100644
--- a/lib/repository/environments.rb
+++ b/lib/repository/environments.rb
@@ -2,7 +2,6 @@
 class Repository::Environments
 
   def self.create(env)
-    env = env
     environments[env] = Repository::Repository.new
     yield(environments[env]) if block_given?
   end
-- 
GitLab