diff --git a/app/controllers/results_controller.rb b/app/controllers/results_controller.rb new file mode 100644 index 000000000..4bf37375f --- /dev/null +++ b/app/controllers/results_controller.rb @@ -0,0 +1,45 @@ +class ResultsController < ApplicationController + before_action :load_vars + + def destroy + can_destroy_result_permission + act_log = t('my_modules.module_archive.table_log', + user: current_user.name, + result: @result.name, + date: l(Time.current, format: :full_date)) + act_log = t('my_modules.module_archive.text_log', + user: current_user.name, + result: @result.name, + date: l(Time.current, format: :full_date)) if @result.is_text + act_log = t('my_modules.module_archive.asset_log', + user: current_user.name, + result: @result.name, + date: l(Time.current, format: :full_date)) if @result.is_asset + + Activity.create( + type_of: :destroy_result, + user: current_user, + project: @my_module.experiment.project, + my_module: @my_module, + message: act_log + ) + flash[:success] = t('my_modules.module_archive.delete_flash', + result: @result.name, + module: @my_module.name) + @result.destroy + redirect_to archive_my_module_path(@my_module) + end + + private + + def load_vars + @result = Result.find_by_id(params[:id]) + @my_module = @result.my_module + end + + def can_destroy_result_permission + unless can_delete_module_result(@result) + render_403 + end + end +end diff --git a/app/helpers/permission_helper.rb b/app/helpers/permission_helper.rb index 93941d8fb..09569a506 100644 --- a/app/helpers/permission_helper.rb +++ b/app/helpers/permission_helper.rb @@ -563,6 +563,9 @@ module PermissionHelper ) end + def can_delete_module_result(result) + is_owner_of_project(result.my_module.experiment.project) + end # ---- RESULT TEXT PERMISSIONS ---- def can_create_result_text_in_module(my_module) diff --git a/app/models/activity.rb b/app/models/activity.rb index c0a088530..bc2c91212 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -38,7 +38,8 @@ class Activity < ActiveRecord::Base :edit_step_comment, :delete_step_comment, :edit_result_comment, - :delete_result_comment + :delete_result_comment, + :destroy_result ] validates :type_of, presence: true diff --git a/app/views/my_modules/archive/_result.html.erb b/app/views/my_modules/archive/_result.html.erb index c96415cf4..70d7d9f9f 100644 --- a/app/views/my_modules/archive/_result.html.erb +++ b/app/views/my_modules/archive/_result.html.erb @@ -16,6 +16,17 @@ <%= link_to option_text, result_table_download_path(result.result_table_id), data: {no_turbolink: true} %> <% end %> + <% if can_delete_module_result(result) %> +
  • + <%= link_to t('my_modules.module_archive.option_delete'), + result_path(action: :delete, id: result.id), + data: { + no_turbolink: true , + confirm: t('my_modules.module_archive.confirm_delete') + }, + method: :delete %> +
  • + <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 3c04b5b01..e63055588 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -464,6 +464,12 @@ en: head_title: "%{project} | %{module} | Sample library" module_archive: head_title: "%{project} | %{module} | Archive" + option_delete: "Delete" + confirm_delete: "Are you sure you want to permanently delete result?" + delete_flash: "Sucessfully removed result %{result} from task %{module}." + table_log: "%{user} deleted table result %{result} on %{date}" + text_log: "%{user} deleted text result %{result} on %{date}" + asset_log: "%{user} deleted file result %{result} on %{date}" archived_on: "Archived on" archived_on_title: "Result archived on %{date} at %{time}." option_download: "Download" diff --git a/config/routes.rb b/config/routes.rb index aa164cc39..b45b67444 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -188,7 +188,7 @@ Rails.application.routes.draw do end end - resources :results, only: [:update] do + resources :results, only: [:update, :destroy] do resources :result_comments, path: '/comments', only: [:new, :create, :index, :edit, :update, :destroy]