From 600a934103270906bf5ebb173d30ec5186209db5 Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Mon, 16 Nov 2015 11:13:30 -0200
Subject: [PATCH] move some institution repository logic for explicity methods

---
 app/builders/institution_builder.rb               |  2 --
 .../orient_db/institution_repository.rb           | 15 +++++++++++----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/app/builders/institution_builder.rb b/app/builders/institution_builder.rb
index 294c4ffd4..df639251a 100644
--- a/app/builders/institution_builder.rb
+++ b/app/builders/institution_builder.rb
@@ -10,9 +10,7 @@ class InstitutionBuilder
     institutions.each do |institution|
       unless institution['@rid'].blank?
         o = Rails.cache.fetch(cache_key(institution['@rid'], institution['last_modified'])) unless institution['last_modified'].blank?
-
         o = institution_repository.find institution['@rid'] if o.nil?
-
         array << o
       end
     end
diff --git a/app/repositories/orient_db/institution_repository.rb b/app/repositories/orient_db/institution_repository.rb
index cee684a7f..56d841fdb 100644
--- a/app/repositories/orient_db/institution_repository.rb
+++ b/app/repositories/orient_db/institution_repository.rb
@@ -4,7 +4,7 @@ module OrientDb
 
     def create(institution)
       institution = connection.command sprintf("INSERT INTO Institution (name, created_at, last_modified, country, city, description, thumbnail, address) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
-        institution.name, Time.now, Time.now, institution.country, institution.city, institution.description, institution.thumbnail, institution.address )
+                                               institution.name, Time.now, Time.now, institution.country, institution.city, institution.description, institution.thumbnail, institution.address)
 
       find(institution["result"][0]["@rid"]) || false
     end
@@ -21,16 +21,23 @@ module OrientDb
     # else return nil
     #
     def associated_with(object)
-      return InstitutionBuilder.build get_edges_end('BelongsTo', 'out', object.rid) if object.class == User
-      return User.find(get_edges_end('BelongsTo', 'in', object.id).map{|user| user['p_id']}) if object.class == Institution
+      return associated_institutions(object) if object.class == User
+      return associated_users(object) if object.class == Institution
       return nil
     end
 
+    def associated_institutions(user)
+      InstitutionBuilder.build get_edges_end('BelongsTo', 'out', user.rid)
+    end
+
+    def associated_users(institution)
+      User.find(get_edges_end('BelongsTo', 'in', institution.id).map { |user| user['p_id'] })
+    end
+
     def add_user(user, institution)
       unless edge_exists? "BelongsTo", user.rid, institution.id
         create_edge "BelongsTo", user.rid, institution.id
       end
-
       institution
     end
 
-- 
GitLab