Skip to content

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

apply NoCreate to WikiPageAttachmentDependentService

ulferts9d8c2932fe44
diff --git a/app/services/projects/copy/wiki_dependent_service.rb b/app/services/projects/copy/wiki_dependent_service.rb
index bc71ed2383a..88c1f336e00 100644
--- a/app/services/projects/copy/wiki_dependent_service.rb
+++ b/app/services/projects/copy/wiki_dependent_service.rb
@@ -74,7 +74,8 @@ module Projects::Copy
.new(user:, model: source_page, contract_class: WikiPages::CopyContract)
.call(wiki: target.wiki,
parent_id: new_parent_id,
- send_notifications: ActionMailer::Base.perform_deliveries)
+ send_notifications: ActionMailer::Base.perform_deliveries,
+ attachments: copy_attachments?)
if service_call.success?
service_call.result
@@ -125,5 +126,9 @@ module Projects::Copy
end
end
end
+
+ def copy_attachments?
+ (params.dig(:params, :only) || []).any? { |k| k.to_sym == :wiki_page_attachments }
+ end
end
end
diff --git a/app/services/projects/copy/wiki_page_attachments_dependent_service.rb b/app/services/projects/copy/wiki_page_attachments_dependent_service.rb
index d4f18c75bd0..8429b203a55 100644
--- a/app/services/projects/copy/wiki_page_attachments_dependent_service.rb
+++ b/app/services/projects/copy/wiki_page_attachments_dependent_service.rb
@@ -28,7 +28,7 @@
module Projects::Copy
class WikiPageAttachmentsDependentService < Dependency
- include ::Copy::Concerns::CopyAttachments
+ include ::Projects::Copy::NoCreate
def self.human_name
I18n.t(:label_wiki_page_attachments)
@@ -37,16 +37,5 @@ module Projects::Copy
def source_count
source.wiki && source.wiki.pages.joins(:attachments).count('attachments.id')
end
-
- protected
-
- def copy_dependency(params:)
- # If no wiki pages copied, we cannot copy their attachments
- return unless state.wiki_page_id_lookup
-
- state.wiki_page_id_lookup.each do |old_id, new_id|
- copy_attachments('WikiPage', from_id: old_id, to_id: new_id)
- end
- end
end
end
diff --git a/app/services/wiki_pages/copy_service.rb b/app/services/wiki_pages/copy_service.rb
index 4730801629e..ccf27755fa2 100644
--- a/app/services/wiki_pages/copy_service.rb
+++ b/app/services/wiki_pages/copy_service.rb
@@ -29,6 +29,7 @@
class WikiPages::CopyService
include ::Shared::ServiceContext
include Contracted
+ include ::Copy::Concerns::CopyAttachments
attr_accessor :user,
:model,
@@ -40,18 +41,24 @@ class WikiPages::CopyService
self.contract_class = contract_class
end
- def call(send_notifications: true, **attributes)
+ def call(send_notifications: true, attachments: true, **attributes)
in_context(model, send_notifications) do
- copy(attributes)
+ copy(attributes, attachments)
end
end
protected
- def copy(attribute_override)
+ def copy(attribute_override, attachments)
attributes = copied_attributes(attribute_override)
- create(attributes)
+ copied_call = create(attributes)
+
+ if copied_call.success? && attachments
+ copy_attachments('WikiPage', from_id: model.id, to_id: copied_call.result.id)
+ end
+
+ copied_call
end
def create(attributes)
diff --git a/spec/services/wiki_pages/copy_service_integration_spec.rb b/spec/services/wiki_pages/copy_service_integration_spec.rb
index 7a76460cdbd..38910f1befa 100644
--- a/spec/services/wiki_pages/copy_service_integration_spec.rb
+++ b/spec/services/wiki_pages/copy_service_integration_spec.rb
@@ -95,6 +95,35 @@ describe WikiPages::CopyService, 'integration', type: :model do
it 'sets the author to be the current user' do
expect(copy.content.author).to eq(user)
end
+
+ context 'with attachments' do
+ let!(:attachment) do
+ create(:attachment,
+ container: wiki_page)
+ end
+
+ context 'when specifying to copy attachments (default)' do
+ it 'copies the attachment' do
+ expect(copy.attachments.length)
+ .to eq 1
+
+ expect(copy.attachments.first.attributes.slice(:digest, :file, :filesize))
+ .to eq attachment.attributes.slice(:digest, :file, :filesize)
+
+ expect(copy.attachments.first.id)
+ .not_to eq attachment.id
+ end
+ end
+
+ context 'when specifying to not copy attachments' do
+ let(:attributes) { { attachments: false } }
+
+ it 'copies the attachment' do
+ expect(copy.attachments.length)
+ .to eq 0
+ end
+ end
+ end
end
describe 'to a different wiki' do