From 3468b84a7bc7f7f26f92bbcfc1c9ac27600bdca7 Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Tue, 20 Oct 2015 08:48:30 -0200
Subject: [PATCH] now, users can create collections

---
 app/controllers/collections_controller.rb     |  7 ++-
 app/models/bookmarks.rb                       |  4 ++
 app/models/collection.rb                      |  8 ++--
 app/models/user.rb                            |  1 -
 app/repositories/orient_db/base.rb            |  1 +
 .../orient_db/collection_repository.rb        | 47 ++++++++++---------
 app/views/collections/_collection.html.erb    |  2 +-
 app/views/collections/_form.html.erb          |  2 -
 config/locales/pt-BR.yml                      |  8 +++-
 9 files changed, 46 insertions(+), 34 deletions(-)

diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb
index ab65c750c..bb580dcf2 100644
--- a/app/controllers/collections_controller.rb
+++ b/app/controllers/collections_controller.rb
@@ -1,10 +1,11 @@
 class CollectionsController < ApplicationController
   before_action :set_collection, only: [:show, :update, :destroy, :like]
+  before_action :authenticate_user!, except: [:index, :show]
 
   # GET /collections
   # GET /collections.json
   def index
-    @collections = collection_repository.all
+    @collections = collection_repository.all current_user
   end
 
   # GET /collections/1
@@ -22,6 +23,7 @@ class CollectionsController < ApplicationController
   # POST /collections.json
   def create
     @collection = Collection.new(collection_params)
+    @collection.owner = current_user
 
     respond_to do |format|
       if collection_repository.create @collection
@@ -65,7 +67,4 @@ class CollectionsController < ApplicationController
     params.require(:collection).permit(:name, :description, learning_objects: [])
   end
 
-  def collection
-  end
-
 end
diff --git a/app/models/bookmarks.rb b/app/models/bookmarks.rb
index 72fc563b1..da16e2d62 100644
--- a/app/models/bookmarks.rb
+++ b/app/models/bookmarks.rb
@@ -8,4 +8,8 @@ class Bookmarks < Collection
     raise 'Bookmarks can`t change the name.'
   end
 
+  def valid?
+    @owner.is_a? User
+  end
+
 end
diff --git a/app/models/collection.rb b/app/models/collection.rb
index 372b236c9..7b259a766 100644
--- a/app/models/collection.rb
+++ b/app/models/collection.rb
@@ -2,12 +2,10 @@ class Collection
   include ActiveModel::Model
   attr_accessor :id, :created_at, :last_modified, :name, :description, :privacy,
                 :owner, :learning_objects
-
   validates_presence_of :name, :created_at, :owner, :learning_objects
-  validates_with Validators::CollectionOwnerValidator
 
   def initialize(params = {})
-    super(params.merge(defaults))
+    super(defaults.merge(params))
   end
 
   ##
@@ -35,6 +33,10 @@ class Collection
     learning_objects.delete learning_object
   end
 
+  def valid?
+    !@name.blank?
+  end
+
   private
 
   def defaults
diff --git a/app/models/user.rb b/app/models/user.rb
index 2b6fdd7ff..8d183743f 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -29,7 +29,6 @@ class User < ActiveRecord::Base
   after_create SyncUserRepositoryService.new
   after_destroy SyncUserRepositoryService.new
 
-  after_create CreateUserBookmarksService.new
   after_destroy CreateUserBookmarksService.new
 
   has_attached_file :avatar, styles: {medium: "300x300>", thumb: "60x60>"}, default_url: lambda { |image| ActionController::Base.helpers.asset_path('user-anon.png') }
diff --git a/app/repositories/orient_db/base.rb b/app/repositories/orient_db/base.rb
index 2acfcab6f..2058b678b 100644
--- a/app/repositories/orient_db/base.rb
+++ b/app/repositories/orient_db/base.rb
@@ -51,6 +51,7 @@ class OrientDb::Base
       var_name = sanitize_orientdb_vars(var)
       hash[var_name] = sanitize_orientdb_values(object.instance_variable_get(var))
     end
+    hash.delete('id')
     hash
   end
 
diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb
index 656494211..63b51407f 100644
--- a/app/repositories/orient_db/collection_repository.rb
+++ b/app/repositories/orient_db/collection_repository.rb
@@ -3,20 +3,17 @@ module OrientDb
     include OrientDb::Methods::EdgeMethods
 
     def build_object(args={})
-      Collection.new(args)
+      Collection.new(map_object_hash(args))
     end
 
     def create_bookmarks_collection(user)
       bookmarks = Bookmarks.new(owner: user)
-
-      if !has_bookmarks?(user)
-        create bookmarks
-      end
+      create bookmarks
     end
 
     def destroy_bookmarks_collection(user)
       bookmarks = user.bookmarks
-      if has_bookmarks? bookmarks
+      if has_bookmarks? user
         destroy bookmarks
       end
     end
@@ -24,6 +21,7 @@ module OrientDb
     def create(object)
       super(object)
       create_edge "BelongsTo", object.id, object.owner.rid
+      object
     end
 
     def destroy(object)
@@ -32,40 +30,45 @@ module OrientDb
     end
 
     def all(user)
-      build_objects(connection.query(sprintf("select expand(in('BelongsTo')) from %s", user.rid)))
+      objects = build_objects get_edges_end('BelongsTo', 'in', user.rid)
     end
 
     def bookmarks(user)
-      result = connection.query(sprintf("select expand(in('BelongsTo')) from %s where name = '%s'", user.rid, 'Bookmarks'))
-
+      # TODO: change bookmarks query
+      result = all user
       if !result.empty?
-        return build_object(result.first)
+        return result.first
       end
-
-      # returns a null object
-      Bookmarks.new(owner: user)
+      create_bookmarks_collection(user)
     end
 
     def has_bookmarks?(user)
-      # checks if bookmarks id is defined
-      if user.bookmarks.id.nil?
-        return false
+      if !user.bookmarks.id.nil? && !user.rid.nil?
+        edge_exists? 'BelongsTo', user.bookmarks.id, user.rid
       end
-
-      # checks if edge exists
-      edge_exists? 'BelongsTo', user.bookmarks.id, user.rid
     end
 
     def build_hash(object)
       hash = super(object)
-      hash.delete("owner")
-      hash.merge('name' => object.name)
+      hash.delete("owner") # delete owner, because it represents an edge in schema
+      hash.merge('name' => object.name) # forces name property for bookmarks
     end
 
     private
 
+    def map_object_hash(hash={})
+      {
+          created_at: hash['created_at'],
+          last_modified: hash['last_modified'],
+          learning_objects: hash['created_at'],
+          privacy: hash['privacy'],
+          name: hash['name'],
+          id: hash['@rid']
+      }
+    end
+
     def odb_class
-      "Collection"
+      'Collection'
     end
 
   end
diff --git a/app/views/collections/_collection.html.erb b/app/views/collections/_collection.html.erb
index be3a0dae7..6f630a4d6 100644
--- a/app/views/collections/_collection.html.erb
+++ b/app/views/collections/_collection.html.erb
@@ -1,4 +1,4 @@
-<%= link_to collection, do %>
+<%= link_to collection do %>
     <%= image_tag 'icons/collection', width: 24 %>
     <%= collection.name %>
 <% end %>
\ No newline at end of file
diff --git a/app/views/collections/_form.html.erb b/app/views/collections/_form.html.erb
index 42bd3cfb3..87e3d99ac 100644
--- a/app/views/collections/_form.html.erb
+++ b/app/views/collections/_form.html.erb
@@ -1,7 +1,5 @@
 <%= form_for collection do |f| %>
-
     <%= f.label :name %>
     <%= f.text_field :name, required: true, style: 'width: 250px;' %>
-
     <%= f.submit %>
 <% end %>
diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml
index dd5d3281a..783f03dc6 100644
--- a/config/locales/pt-BR.yml
+++ b/config/locales/pt-BR.yml
@@ -107,6 +107,7 @@ pt-BR:
     models:
       attribute: "Atributo"
       carousel: "Carossel"
+      bookmarks: "Favoritos"
       collection: "Coleção"
       institution: "Instituição"
       learning_object: "Objeto Educacional"
@@ -123,6 +124,11 @@ pt-BR:
         password_confirmation: "Confirme sua senha"
         current_password: "Senha atual"
         remember_me: "lembrar de mim?"
+      bookmarks:
+        name: "Nome"
+        description: "Descrição"
+        privacy: "Privacidade"
+        learning_objects: "Objetos Educacionais"
     errors:
       template:
         header:
@@ -149,4 +155,4 @@ pt-BR:
         less_than: "precisa ser menor do que {{count}}"
         less_than_or_equal_to: "precisa ser menor ou igual a {{count}}"
         odd: "precisa ser ímpar"
-        even: "precisa ser par"
+        even: "precisa ser par"
\ No newline at end of file
-- 
GitLab