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

Replace wiki page attach_files with attachable services

Oliver Günthera59e4dc376f3
diff --git a/app/contracts/wiki_pages/update_contract.rb b/app/contracts/wiki_pages/update_contract.rb
new file mode 100644
index 00000000000..3aa737381de
--- /dev/null
+++ b/app/contracts/wiki_pages/update_contract.rb
@@ -0,0 +1,32 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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-2013 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 WikiPages
+ class UpdateContract < BaseContract
+ end
+end
diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb
index 8ceed253714..b003b9d9be5 100644
--- a/app/controllers/wiki_controller.rb
+++ b/app/controllers/wiki_controller.rb
@@ -57,6 +57,7 @@ class WikiController < ApplicationController
before_action :handle_new_wiki_page, only: %i[show]
before_action :build_wiki_page_and_content, only: %i[new create]
+ include AttachableServiceCall
include AttachmentsHelper
include PaginationHelper
include Redmine::MenuManager::WikiMenuHelper
@@ -105,13 +106,13 @@ class WikiController < ApplicationController
end
def create
- @page.attributes = permitted_params.wiki_page
+ call = attachable_create_call ::WikiPages::CreateService,
+ args: permitted_params.wiki_page_with_content.to_h.merge(wiki: @wiki)
- @content.attributes = permitted_params.wiki_content
- @content.author = User.current
- @page.attach_files(permitted_params.attachments.to_h)
+ @page = call.result
+ @content = @page.content
- if @page.save
+ if call.success?
call_hook(:controller_wiki_edit_after_save, params: params, page: @page)
flash[:notice] = I18n.t(:notice_successful_create)
redirect_to_show
@@ -165,18 +166,16 @@ class WikiController < ApplicationController
return
end
- @content = @page.content || @page.build_content
return if locked?
- @page.attach_files(permitted_params.attachments.to_h)
+ call = attachable_update_call ::WikiPages::UpdateService,
+ model: @page,
+ args: permitted_params.wiki_page_with_content.to_h
- @page.title = permitted_params.wiki_page[:title]
- @page.parent_id = permitted_params.wiki_page[:parent_id].presence
- @content.attributes = permitted_params.wiki_content
- @content.author = User.current
- @content.add_journal User.current, params['content']['comments']
+ @page = call.result
+ @content = @page.content
- if @page.save_with_content
+ if call.success?
call_hook(:controller_wiki_edit_after_save, params: params, page: @page)
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to_show
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index 397d8123b26..cb242b4c6ec 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -252,6 +252,10 @@ class PermittedParams
params.require(:content).permit(*self.class.permitted_attributes[:wiki_content])
end
+ def wiki_page_with_content
+ wiki_page.merge(wiki_content)
+ end
+
def pref
params.fetch(:pref, {}).permit(:hide_mail, :time_zone, :theme,
:comments_sorting, :warn_on_leaving_unsaved,
diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rb
index 772b5ef3f2d..33f00f1586c 100644
--- a/app/services/attachments/create_service.rb
+++ b/app/services/attachments/create_service.rb
@@ -68,7 +68,6 @@ class Attachments::CreateService < ::BaseServices::Create
def error_wrapped_call
yield
rescue StandardError => e
- binding.pry
log_attachment_saving_error(e)
message =
diff --git a/app/services/wiki_pages/create_service.rb b/app/services/wiki_pages/create_service.rb
index 6c138267d6e..8fd4e802d5b 100644
--- a/app/services/wiki_pages/create_service.rb
+++ b/app/services/wiki_pages/create_service.rb
@@ -29,4 +29,5 @@
#++
class WikiPages::CreateService < ::BaseServices::Create
+ include Attachments::ReplaceAttachments
end
diff --git a/app/services/wiki_pages/set_attributes_service.rb b/app/services/wiki_pages/set_attributes_service.rb
index c3b89e7c115..64fd698d346 100644
--- a/app/services/wiki_pages/set_attributes_service.rb
+++ b/app/services/wiki_pages/set_attributes_service.rb
@@ -36,6 +36,8 @@
#
# Attributes for both the page as well as for the content are accepted.
class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
+ include Attachments::SetReplacements
+
private
def set_attributes(params)
@@ -53,7 +55,7 @@ class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
end
def set_default_attributes(_params)
- model.build_content
+ model.build_content page: model
model.content.extend(OpenProject::ChangedBySystem)
model.content.change_by_system do
@@ -66,6 +68,10 @@ class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
end
def split_page_and_content_params(params)
- params.partition { |p, _| WikiContent.column_names.include?(p) }.map(&:to_h)
+ params.partition { |p, _| content_attribute?(p) }.map(&:to_h)
+ end
+
+ def content_attribute?(name)
+ WikiContent.column_names.include?(name) || name.to_s == 'comments'
end
end
diff --git a/app/services/wiki_pages/update_service.rb b/app/services/wiki_pages/update_service.rb
new file mode 100644
index 00000000000..c76009f09ad
--- /dev/null
+++ b/app/services/wiki_pages/update_service.rb
@@ -0,0 +1,50 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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-2013 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 WikiPages::UpdateService < ::BaseServices::Update
+ include Attachments::ReplaceAttachments
+
+ private
+
+ def persist(service_result)
+ service_result = super(service_result)
+
+ page = service_result.result
+ content = page.content
+
+ unless page.save && content.save
+ service_result.errors = page.errors
+ service_result.errors.merge! content.errors
+ service_result.success = false
+ end
+
+ service_result
+ end
+end