From f9b02f39c724beda40def213b870b11af6e4b28f Mon Sep 17 00:00:00 2001
From: Matheus Agio Nerone <man13@inf.ufpr.br>
Date: Tue, 10 Nov 2015 12:06:42 -0200
Subject: [PATCH] now image change when liking or disliking

Signed-off-by: Matheus Agio Nerone <man13@inf.ufpr.br>
---
 .../application/learning_objects.coffee.erb   | 55 +++++++++++++++++++
 .../learning_objects_controller.rb            |  1 +
 app/views/learning_objects/show.html.erb      |  6 +-
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 app/assets/javascripts/application/learning_objects.coffee.erb

diff --git a/app/assets/javascripts/application/learning_objects.coffee.erb b/app/assets/javascripts/application/learning_objects.coffee.erb
new file mode 100644
index 00000000..20f241eb
--- /dev/null
+++ b/app/assets/javascripts/application/learning_objects.coffee.erb
@@ -0,0 +1,55 @@
+$ ->
+  $('.default_btn').toggleClass 'active_btn'
+  $(document).on 'click', '*[data-poload]', ->
+    e = $(this)
+    e.popover({content: "Carregando..."}).popover('show')
+    all = undefined
+    objects = undefined
+    url = $(this).data('poload')
+    loid = $(this).data('loid')
+    $.get '/collections.json', (d) ->
+      all = d
+      $.get url, (d) ->
+        object = d
+
+        # generate a lookup table for object's collections id
+        lookup = {}
+        i = 0
+        len = object.length
+        while i < len
+          lookup[object[i].id] = object[i]
+          i++
+
+        # mark checked objects
+        i = 0
+        len = all.length
+        html = ''
+        while i < len
+          name = 'collection_' + all[i].id.split(':').slice(-1)[0]
+          html += '<input type="checkbox" class="collection-element" data-cid="' + all[i].id + '" data-loid="' + loid + '" id="' + name + '" value="' + all[i].id + '"'
+          if lookup[all[i].id] != undefined
+            html += " checked"
+          html += '>'
+          html += '<label for="' + name + '">' + all[i].name + '</label><br/>'
+          ++i
+
+        $('.popover-content').html(html)
+      return
+    return
+  return
+
+$ ->
+  $(document).on 'click', '.add_to_collection', (e) ->
+    $('.add_to_collection').removeClass 'active'
+    $('.add_to_collection').not(this).popover 'hide'
+    return
+$ ->
+  $(document).on 'ajax:success', 'a.vote', (status, data, xhr) ->
+    $('.votes-count[data-id=\'' + data.id + '\']').text data.count
+    if $('.vote img').attr('alt') == 'Descurtir'
+      $('.vote img').attr('src',"<%= image_path('icons/Curtir_01.png') %>")
+      $('.vote img').attr('alt', 'Curtir')
+    else
+      $('.vote img').attr('src',"<%= image_path('icons/Curtir_02.png') %>")
+      $('.vote img').attr('alt', 'Descurtir')
+    return
diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb
index f2288acb..b2e41624 100644
--- a/app/controllers/learning_objects_controller.rb
+++ b/app/controllers/learning_objects_controller.rb
@@ -13,6 +13,7 @@ class LearningObjectsController < ApplicationController
   # GET /learning_objects/1
   # GET /learning_objects/1.json
   def show
+    @liked = learning_object_repository.liked?(current_user, @learning_object)
   end
 
   # GET /learning_objects/new
diff --git a/app/views/learning_objects/show.html.erb b/app/views/learning_objects/show.html.erb
index ee0f8edc..07cf7741 100644
--- a/app/views/learning_objects/show.html.erb
+++ b/app/views/learning_objects/show.html.erb
@@ -62,7 +62,11 @@
         <div class="actions">
           <div class="col-md-3 action">
             <%= link_to like_learning_object_path(id: @learning_object.id), class: 'vote',method: :post, remote: true do %>
-                <%= image_tag "icons/Curtir_01.png", alt: "Curtir" %>
+                <% if @liked %>
+                  <%= image_tag "icons/Curtir_02.png", alt: "Descurtir" %>
+                <% else %>
+                  <%= image_tag "icons/Curtir_01.png", alt: "Curtir" %>
+                <% end %>
                 <h6>curtir objeto</h6>
             <% end %>
           </div>
-- 
GitLab