From 4312d5f7dad3513febc99a0017cf4925743745bd Mon Sep 17 00:00:00 2001
From: Mateus Rambo Strey <mars11@inf.ufpr.br>
Date: Tue, 10 Nov 2015 17:05:05 -0200
Subject: [PATCH] add privacy selector in collection view

---
 .../javascripts/application/collections.coffee   |  8 ++++++++
 .../stylesheets/application/collections.scss     |  8 +++++++-
 app/assets/stylesheets/application/welcome.scss  |  1 -
 app/controllers/collections_controller.rb        | 13 +++++++++++--
 app/repositories/orient_db/base.rb               | 16 +++++++++++++---
 .../orient_db/collection_repository.rb           | 12 ++++++++++++
 .../orient_db/learning_object_repository.rb      |  6 ------
 app/views/collections/show.html.erb              |  8 ++++++--
 config/routes.rb                                 |  2 ++
 9 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/app/assets/javascripts/application/collections.coffee b/app/assets/javascripts/application/collections.coffee
index fa82b5ea..21be34e5 100644
--- a/app/assets/javascripts/application/collections.coffee
+++ b/app/assets/javascripts/application/collections.coffee
@@ -17,3 +17,11 @@ $ ->
       $.ajax {method: "DELETE", url: url }, (d) ->
         d
   return
+
+$ ->
+  $(document).on 'click', 'input[name=privacy]', ->
+    url = '/collections/' + encodeURIComponent($(this).data('cid')) + '/change_privacy'
+    value = $('input[name=privacy]:checked').val()
+    $.post url, {'privacy':value}, (d) ->
+      d
+  return
diff --git a/app/assets/stylesheets/application/collections.scss b/app/assets/stylesheets/application/collections.scss
index 53e901f1..3a380078 100644
--- a/app/assets/stylesheets/application/collections.scss
+++ b/app/assets/stylesheets/application/collections.scss
@@ -3,4 +3,10 @@ ul.collection-header {
     width: 100%;
     margin-bottom: 10px;
   }
-}
\ No newline at end of file
+}
+
+.collection-privacy {
+  text-align: center;
+  margin-top: 20px;
+  margin-right: 30px;
+}
diff --git a/app/assets/stylesheets/application/welcome.scss b/app/assets/stylesheets/application/welcome.scss
index 0ab1389a..636fb835 100644
--- a/app/assets/stylesheets/application/welcome.scss
+++ b/app/assets/stylesheets/application/welcome.scss
@@ -2,7 +2,6 @@
   position: relative;
   min-height: 500px;
   min-width: 350px;
-  margin-top: 30px;
   background-color: #2178F5;
   color: white;
 }
diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb
index 7482e6c5..467d8114 100644
--- a/app/controllers/collections_controller.rb
+++ b/app/controllers/collections_controller.rb
@@ -1,6 +1,6 @@
 class CollectionsController < ApplicationController
-  before_action :set_collection, only: [:show, :update, :destroy, :like, :add_learning_object, :remove_learning_object]
-  before_action :authenticate_user!, only: [:update, :destroy, :like, :add_learning_object, :remove_learning_object]
+  before_action :set_collection, only: [:show, :update, :destroy, :like, :add_learning_object, :remove_learning_object, :change_privacy]
+  before_action :authenticate_user!, only: [:update, :destroy, :like, :add_learning_object, :remove_learning_object, :change_privacy]
 
   # GET /collections
   # GET /collections.json
@@ -83,6 +83,15 @@ class CollectionsController < ApplicationController
     end
   end
 
+  # change collection privacy
+  def change_privacy
+    collection_repository.change_privacy(@collection, params[:privacy])
+
+    if request.xhr?
+      render json: {status: true}
+    end
+  end
+
   private
 
   def set_collection
diff --git a/app/repositories/orient_db/base.rb b/app/repositories/orient_db/base.rb
index e6eba5e3..71ee00ee 100644
--- a/app/repositories/orient_db/base.rb
+++ b/app/repositories/orient_db/base.rb
@@ -45,6 +45,12 @@ class OrientDb::Base
     objects
   end
 
+  def update_property(object, property, value)
+    if accepted_properties.include? property
+      connection.command "UPDATE #{odb_class} SET #{property}='#{value}', last_modified='#{Time.now()}' WHERE @rid = #{object.id}"
+    end
+  end
+
   # Take the object and make a hash in the OrientDB format.
   # Used to create a document.
   def build_hash(object)
@@ -77,8 +83,12 @@ class OrientDb::Base
 
   protected
 
-  def odb_class
-    raise NoMethodError, "You must implement this method"
-  end
+    def accepted_properties
+      raise NoMethodError, "You must implement this method"
+    end
+
+    def odb_class
+      raise NoMethodError, "You must implement this method"
+    end
 
 end
diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb
index 91f7bab5..19a5c9c4 100644
--- a/app/repositories/orient_db/collection_repository.rb
+++ b/app/repositories/orient_db/collection_repository.rb
@@ -100,8 +100,20 @@ module OrientDb
       return []
     end
 
+    def change_privacy(collection, privacy)
+      update_property(collection, 'privacy', privacy) if accepted_privacies(privacy)
+    end
+
     private
 
+    def accepted_properties
+      ['privacy']
+    end
+
+    def accepted_privacies(privacy)
+      ['private', 'public'].include? privacy
+    end
+
     def owner_id(owner)
       (owner.class == User) ? owner.rid : owner.id
     end
diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb
index c4ad46b5..c8d1046a 100644
--- a/app/repositories/orient_db/learning_object_repository.rb
+++ b/app/repositories/orient_db/learning_object_repository.rb
@@ -172,12 +172,6 @@ module OrientDb
       end
     end
 
-    def update_property(learning_object, property, value)
-      if accepted_properties.include? property
-        connection.command "UPDATE LearningObject SET #{property}='#{value}', last_modified='#{Time.now()}' WHERE @rid = #{learning_object.id}"
-      end
-    end
-
     def types
       Rails.cache.fetch("learning_object/types", expires_in: 1.days) do
         query = "select type from LearningObject GROUP BY type"
diff --git a/app/views/collections/show.html.erb b/app/views/collections/show.html.erb
index 4d0aee9a..1ae54056 100644
--- a/app/views/collections/show.html.erb
+++ b/app/views/collections/show.html.erb
@@ -1,10 +1,14 @@
 <nav class="navigation navbar-default "><br/>
 
   <div class="container-fluid">
-    <div class="navbar-right">
+    <div class="navbar-right" style="text-align:right">
       <%= link_to '#' do %>
           <span style="margin-right: 30px;"><button class="btn btn-danger btn-remove">APAGAR</button></span>
       <% end %>
+      <div class="collection-privacy">
+        <input type="radio" name="privacy" id="privacy_private" data-cid="<%= @collection.id %>" value="private" <%= "checked" if @collection.privacy == "private" %>><label for="privacy_private">Privada</label> |
+        <input type="radio" name="privacy" id="privacy_public" data-cid="<%= @collection.id %>" value="public" <%= "checked" if @collection.privacy == "public" %>><label for="privacy_public">Pública</label>
+      </div>
     </div>
     <div class="navbar-header">
       <%= image_tag image_path("icons/collection.png"), class: "logo-image", size: "90x66" %>
@@ -45,4 +49,4 @@
       <%= render learning_object, orientation: 'vertical' %>
   <% end %>
 
-</div>
\ No newline at end of file
+</div>
diff --git a/config/routes.rb b/config/routes.rb
index 6b891792..b91e3f66 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -68,6 +68,8 @@ Rails.application.routes.draw do
 
       # remove a learning object for some collection
       delete '/learning_object/:learning_object_id', as: :destroy_learning_object, action: :remove_learning_object
+
+      post :change_privacy
     end
   end
 
-- 
GitLab