diff --git a/app/controllers/learning_objects_controller.rb b/app/controllers/learning_objects_controller.rb index 7af866f1a2d40a4ef5525bb0c81d5c40f36d75b6..f64f40f72a7653bbfa1af6e65a492f2dd8967d5b 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, :like, :bookmarks, + :destroy, :destroy_attachment, :like, :bookmarks, :collections, :upload, :upload_link, :download, :user_not_authorized] after_action :increment_learning_object_views, only: [:show] @@ -81,6 +81,13 @@ class LearningObjectsController < ApplicationController end end + def destroy_attachment + attachment = LearningObject::Attachment.find(params[:attachment_id]) + @learning_object.update(attachment: nil) if @learning_object.attachment == attachment + attachment.destroy + 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 e91c7db7122b4e0b2587d97586ce3dd223ee71f8..5d413e3c6c9f4dcabefbfa3a3b2e0297e95475a2 100644 --- a/app/policies/learning_object_policy.rb +++ b/app/policies/learning_object_policy.rb @@ -26,6 +26,10 @@ class LearningObjectPolicy < ApplicationPolicy record if owns? end + def destroy_attachment? + record if owns? + end + def show? if user.nil? record.state == 'published' diff --git a/app/views/learning_objects/build/_file_upload_form.html.erb b/app/views/learning_objects/build/_file_upload_form.html.erb index 1da81f4fe091fd0679d139bda91580952be07493..2c9c983e27fcf6c563f3106677a765a8d6c529cc 100644 --- a/app/views/learning_objects/build/_file_upload_form.html.erb +++ b/app/views/learning_objects/build/_file_upload_form.html.erb @@ -30,7 +30,7 @@ <tr> <td> <div class="fileName"><%= "#{attachment.name} (#{attachment.mime_type})" %></div> - <div class="deleteFile"><i class="fa fa-trash fa-1"></i></div> + <div class="deleteFile" onclick="removeFile(<%= attachment.id %>, <%= @learning_object.id %>)"><i class="fa fa-trash fa-1"></i></div> </td> </tr> <% end %> @@ -154,6 +154,13 @@ </style> <script type="text/javascript" charset="utf-8"> + function removeFile(file_id, learning_object_id){ + $.ajax({ + url: '/learning_objects/'+learning_object_id+'/remove_file/'+file_id, + type: 'POST' + }) + } + (function () { var r = new Resumable({ target: "<%= chunk_path %>", diff --git a/config/routes.rb b/config/routes.rb index 3a6aa06ca2fa0df06f61e6a951b020bbb5bc468f..97360dff80f42298d301109f7a26d3e02552af96 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,10 +71,10 @@ Rails.application.routes.draw do end end + post 'learning_objects/:id/remove_file/:attachment_id' => 'learning_objects#destroy_attachment', as: 'remove_file' resources :learning_objects do resources :build, controller: 'learning_objects/build' get :reviews, to: 'reviews#list', as: 'reviews' - member do post :like get :collections