From 4d312cb3ffccc13e3e5304033a56b56c555cd543 Mon Sep 17 00:00:00 2001
From: man13 <man13@inf.ufpr.br>
Date: Tue, 29 Mar 2016 11:09:42 -0300
Subject: [PATCH] adding remove thumbnail feature

Signed-off-by: man13 <man13@inf.ufpr.br>
---
 app/controllers/learning_objects_controller.rb    |  8 +++++++-
 app/policies/learning_object_policy.rb            |  4 ++++
 .../build/select_thumbnail.html.erb               | 15 +++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb
index 0b45c23f..41978b12 100644
--- a/app/controllers/learning_objects_controller.rb
+++ b/app/controllers/learning_objects_controller.rb
@@ -6,7 +6,7 @@ class LearningObjectsController < ApplicationController
 
   before_action :authenticate_user!, except: [:index, :show]
   before_action :set_learning_object, only: [:show, :edit, :update,
-                                             :destroy, :destroy_attachment, :like, :bookmarks,
+                                             :destroy, :destroy_attachment, :remove_thumbnail, :like, :bookmarks,
                                              :collections, :upload, :upload_link, :download,
                                              :user_not_authorized]
   after_action :increment_learning_object_views, only: [:show]
@@ -88,6 +88,12 @@ class LearningObjectsController < ApplicationController
     render :nothing => true, :status => 204
   end
 
+  def remove_thumbnail
+    @learning_object.thumbnail = nil
+    @learning_object.save
+    render :nothing => true, :status => 204
+  end
+
   def download
     @learning_object.download current_user
 
diff --git a/app/policies/learning_object_policy.rb b/app/policies/learning_object_policy.rb
index 5d413e3c..2237cf79 100644
--- a/app/policies/learning_object_policy.rb
+++ b/app/policies/learning_object_policy.rb
@@ -30,6 +30,10 @@ class LearningObjectPolicy < ApplicationPolicy
     record if owns?
   end
 
+  def remove_thumbnail?
+    record if owns?
+  end
+
   def show?
     if user.nil?
       record.state == 'published'
diff --git a/app/views/learning_objects/build/select_thumbnail.html.erb b/app/views/learning_objects/build/select_thumbnail.html.erb
index a44be272..04f33baa 100644
--- a/app/views/learning_objects/build/select_thumbnail.html.erb
+++ b/app/views/learning_objects/build/select_thumbnail.html.erb
@@ -31,9 +31,24 @@
     <%= form_for @learning_object, url: wizard_path, method: :put, html: { multipart: true } do |f| %>
         <%=  f.file_field :thumbnail, required: true  %>
 
+        <% unless @learning_object.thumbnail.blank? %>
+            <div>Thumbnail: <%= @learning_object.thumbnail_file_name %>&nbsp;<i id="remove_thumbnail" class="fa fa-trash fa-1"></i></div>
+            <script>
+              $("#remove_thumbnail").click(function(){
+                if(confirm("Você tem certeza?")){
+                  $(this).parent().remove()
+                  $.ajax({
+                    url: "<%= remove_thumbnail_path(@learning_object) %>",
+                    method: 'POST'
+                  })
+                }
+              });
+            </script>
+        <% end %>
         <button type="submit" class="btn-submit btn btn-success btn-outline-rounded green">
           Enviar <i style="margin-left:10px;" class="fa fa-paper-plane"></i>
         </button>
+
         <%= link_to next_wizard_path do %>
             <button type="button" class="btn-submit btn btn-warning btn-outline-rounded yellow">Pular etapa</button>
         <% end %>
-- 
GitLab