OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
have a delete service for attachments
ulfertsa895767619a5
diff --git a/app/contracts/attachments/delete_contract.rb b/app/contracts/attachments/delete_contract.rbnew file mode 100644index 00000000000..59a27356d3c--- /dev/null+++ b/app/contracts/attachments/delete_contract.rb@@ -0,0 +1,37 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Attachments+ class DeleteContract < ::DeleteContract+ delete_permission -> {+ model.deletable?(user)+ }+ end+enddiff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rbindex e7135a956f6..c4715c027fc 100644--- a/app/services/attachments/create_service.rb+++ b/app/services/attachments/create_service.rb@@ -40,21 +40,24 @@ class Attachments::CreateService## An ActiveRecord::RecordInvalid error is raised if any record can't be saved.def call(uploaded_file:, description:)- if JournalManager.journalized?(container)- save_journalized(uploaded_file, description)+ if container.nil?+ create_attachment(uploaded_file, description)+ elsif JournalManager.journalized?(container)+ create_journalized(uploaded_file, description)else- save_unjournalized(uploaded_file, description)+ create_unjournalized(uploaded_file, description)endendprivate- def save_journalized(uploaded_file, description)+ def create_journalized(uploaded_file, description)OpenProject::Mutex.with_advisory_lock_transaction(container) do+ attachment = create_attachment(uploaded_file, description)# Get the latest attachments to ensure having them all for journalization.- # A different worker might have added attachments in the meantime, e.g when bulk uploading+ # We just created an attachment and a different worker might have added attachments+ # in the meantime, e.g when bulk uploading.container.attachments.reload- attachment = build_attachment(uploaded_file, description)add_journalsave_container@@ -63,8 +66,8 @@ class Attachments::CreateServiceendend- def save_unjournalized(uploaded_file, description)- build_attachment(uploaded_file, description).tap do+ def create_unjournalized(uploaded_file, description)+ create_attachment(uploaded_file, description).tap dosave_containerendend@@ -73,6 +76,16 @@ class Attachments::CreateServicecontainer.add_journal authorend+ def create_attachment(uploaded_file, description)+ attachment = Attachment.new(file: uploaded_file,+ container: container,+ description: description,+ author: author)++ attachment.save!+ attachment+ end+def build_attachment(uploaded_file, description)container.attachments.build(file: uploaded_file, description: description, author: author)enddiff --git a/app/services/attachments/delete_service.rb b/app/services/attachments/delete_service.rbnew file mode 100644index 00000000000..38c4fa489ae--- /dev/null+++ b/app/services/attachments/delete_service.rb@@ -0,0 +1,45 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class Attachments::DeleteService < ::BaseServices::Delete+ def call(params = nil)+ in_context(model.container || model) do+ perform(params)+ end+ end++ private++ def destroy(attachment)+ if attachment.container+ attachment.container.attachments.delete(attachment)+ else+ super+ end+ end+enddiff --git a/app/services/base_services/delete.rb b/app/services/base_services/delete.rbindex 184d66bc4c4..ccdd8c06935 100644--- a/app/services/base_services/delete.rb+++ b/app/services/base_services/delete.rb@@ -38,7 +38,7 @@ module BaseServicesdef persist(service_result)service_result = super(service_result)- unless service_result.result.destroy+ unless destroy(service_result.result)service_result.errors = service_result.result.errorsservice_result.success = falseend@@ -46,6 +46,10 @@ module BaseServicesservice_resultend+ def destroy(object)+ object.destroy+ end+protecteddef default_contract_classdiff --git a/app/services/shared/service_context.rb b/app/services/shared/service_context.rbindex 1e8cdb85186..e7d5b367566 100644--- a/app/services/shared/service_context.rb+++ b/app/services/shared/service_context.rb@@ -32,7 +32,7 @@ module Sharedmodule ServiceContextprivate- def in_context(model, send_notifications, &block)+ def in_context(model, send_notifications = true, &block)if modelin_mutex_context(model, send_notifications, &block)else@@ -40,13 +40,13 @@ module Sharedendend- def in_mutex_context(model, send_notifications, &block)+ def in_mutex_context(model, send_notifications = true, &block)OpenProject::Mutex.with_advisory_lock_transaction(model) doin_user_context(send_notifications, &block)endend- def in_user_context(send_notifications)+ def in_user_context(send_notifications = true)result = nilActiveRecord::Base.transaction dodiff --git a/lib/api/v3/attachments/attachments_api.rb b/lib/api/v3/attachments/attachments_api.rbindex 44485ebb8e9..3e0fbf7add6 100644--- a/lib/api/v3/attachments/attachments_api.rb+++ b/lib/api/v3/attachments/attachments_api.rb@@ -59,17 +59,7 @@ module APIAttachmentRepresenter.new(@attachment, embed_links: true, current_user: current_user)end- delete do- raise API::Errors::Unauthorized unless @attachment.deletable?(current_user)-- if @attachment.container- @attachment.container.attachments.delete(@attachment)- else- @attachment.destroy- end-- status 204- end+ delete &::API::V3::Utilities::Endpoints::Delete.new(model: Attachment).mountnamespace :content dohelpers ::API::Helpers::AttachmentRendererdiff --git a/spec/contracts/time_entries/delete_contract_spec.rb b/spec/contracts/time_entries/delete_contract_spec.rbindex c9e4e9ae2a8..e2efff47bd7 100644--- a/spec/contracts/time_entries/delete_contract_spec.rb+++ b/spec/contracts/time_entries/delete_contract_spec.rb@@ -54,13 +54,13 @@ describe TimeEntries::DeleteContract dolet(:time_entry) doFactoryBot.build_stubbed(:time_entry,- project: time_entry_project,- work_package: time_entry_work_package,- user: time_entry_user,- activity: time_entry_activity,- spent_on: time_entry_spent_on,- hours: time_entry_hours,- comments: time_entry_comments)+ project: time_entry_project,+ work_package: time_entry_work_package,+ user: time_entry_user,+ activity: time_entry_activity,+ spent_on: time_entry_spent_on,+ hours: time_entry_hours,+ comments: time_entry_comments)endbefore dodiff --git a/spec/services/attachments/create_service_spec.rb b/spec/services/attachments/create_service_spec.rbindex 9ccc80f74f0..adb1e188c15 100644--- a/spec/services/attachments/create_service_spec.rb+++ b/spec/services/attachments/create_service_spec.rb@@ -34,7 +34,7 @@ describe Attachments::CreateService dolet(:container) { work_package }let(:description) { 'a fancy description' }- subject { described_class.new(work_package, author: user) }+ subject { described_class.new(container, author: user) }describe '#call' dodef call_tested_method@@ -50,12 +50,12 @@ describe Attachments::CreateService doendit 'adds the attachment to the WP' do- work_package.reload- expect(work_package.attachments).to include Attachment.first+ container.reload+ expect(container.attachments).to include Attachment.firstendit 'adds a journal entry on the WP' do- expect(work_package.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment+ expect(container.journals.count).to eq 2 # 1 for WP creation + 1 for the attachmentendend@@ -78,6 +78,20 @@ describe Attachments::CreateService doit_behaves_like 'successful creation'end+ context "uncontainered" do+ let(:container) { nil }++ before do+ call_tested_method+ end++ it 'saves the attachment' do+ attachment = Attachment.first+ expect(attachment.filename).to eq 'foobar.txt'+ expect(attachment.description).to eq description+ end+ end+context 'invalid attachment', with_settings: { attachment_max_size: 0 } doit 'raises the exception' doexpect { call_tested_method }