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

Convert document create/update into services

Oliver Günther0fce502a69b2
diff --git a/app/controllers/concerns/attachable_service_call.rb b/app/controllers/concerns/attachable_service_call.rb
new file mode 100644
index 00000000000..5803a95b191
--- /dev/null
+++ b/app/controllers/concerns/attachable_service_call.rb
@@ -0,0 +1,66 @@
+#-- 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 AttachableServiceCall
+ ##
+ # Call the presented CreateContract service
+ # with the given params, merging in any attachment params
+ #
+ # @param service_cls the service class instance
+ # @param args permitted args for the service call
+ def attachable_create_call(service_cls, args:)
+ service_cls
+ .new(user: current_user)
+ .call(args.merge(attachment_params))
+ end
+
+ ##
+ # Call the presented UpdateContract service
+ # with the given params, merging in any attachment params
+ #
+ # @param service_cls the service class instance
+ # @param args permitted args for the service call
+ def attachable_update_call(service_cls, model:, args:)
+ service_cls
+ .new(user: current_user, model: model)
+ .call(args.merge(attachment_params))
+ end
+
+ ##
+ # Attachable parameters mapped to a format the
+ # SetReplacements service concern
+ def attachment_params
+ attachment_params = permitted_params.attachments.to_h
+
+ if attachment_params.any?
+ { attachment_ids: attachment_params.values.map(&:values).flatten }
+ else
+ {}
+ end
+ end
+end
diff --git a/modules/documents/app/contracts/documents/base_contract.rb b/modules/documents/app/contracts/documents/base_contract.rb
new file mode 100644
index 00000000000..0670ef94a0f
--- /dev/null
+++ b/modules/documents/app/contracts/documents/base_contract.rb
@@ -0,0 +1,54 @@
+#-- 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.
+#++
+
+module Documents
+ class BaseContract < ::ModelContract
+ include Attachments::ValidateReplacements
+
+ def self.model
+ Document
+ end
+
+ attribute :project
+ attribute :category
+ attribute :title
+ attribute :description
+
+ validate :validate_manage_allowed
+
+ private
+
+ def validate_manage_allowed
+ unless user.allowed_to?(:manage_documents, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+end
diff --git a/modules/documents/app/contracts/documents/create_contract.rb b/modules/documents/app/contracts/documents/create_contract.rb
new file mode 100644
index 00000000000..29af7bcec61
--- /dev/null
+++ b/modules/documents/app/contracts/documents/create_contract.rb
@@ -0,0 +1,34 @@
+#-- 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.
+#++
+
+module Documents
+ class CreateContract < BaseContract
+ end
+end
diff --git a/modules/documents/app/contracts/documents/update_contract.rb b/modules/documents/app/contracts/documents/update_contract.rb
new file mode 100644
index 00000000000..1c83f98988e
--- /dev/null
+++ b/modules/documents/app/contracts/documents/update_contract.rb
@@ -0,0 +1,34 @@
+#-- 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.
+#++
+
+module Documents
+ class UpdateContract < BaseContract
+ end
+end
diff --git a/modules/documents/app/controllers/documents_controller.rb b/modules/documents/app/controllers/documents_controller.rb
index f805cc106e7..86b58661f7e 100644
--- a/modules/documents/app/controllers/documents_controller.rb
+++ b/modules/documents/app/controllers/documents_controller.rb
@@ -29,6 +29,7 @@
#++
class DocumentsController < ApplicationController
+ include AttachableServiceCall
default_search_scope :documents
model_object Document
before_action :find_project_by_project_id, only: %i[index new create]
@@ -64,14 +65,14 @@ class DocumentsController < ApplicationController
end
def create
- @document = @project.documents.build
- @document.attributes = document_params
- @document.attach_files(permitted_params.attachments.to_h)
+ call = attachable_create_call ::Documents::CreateService,
+ args: document_params.merge(project: @project)
- if @document.save
+ if call.success?
flash[:notice] = I18n.t(:notice_successful_create)
redirect_to project_documents_path(@project)
else
+ @document = call.result
render action: 'new'
end
end
@@ -81,13 +82,15 @@ class DocumentsController < ApplicationController
end
def update
- @document.attributes = document_params
- @document.attach_files(permitted_params.attachments.to_h)
+ call = attachable_update_call ::Documents::UpdateService,
+ model: @document,
+ args: document_params
- if @document.save
+ if call.success?
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to action: 'show', id: @document
else
+ @document = call.result
render action: 'edit'
end
end
@@ -98,18 +101,19 @@ class DocumentsController < ApplicationController
end
def add_attachment
- @document.attach_files(permitted_params.attachments.to_h)
- attachments = @document.attachments.select(&:new_record?)
+ current_attachments = @document.attachments.pluck(:id)
+ call = attachable_update_call ::Documents::UpdateService,
+ model: @document,
+ args: document_params
- @document.save
- saved_attachments = attachments.select(&:persisted?)
+ if call.success? && Setting.notified_events.include?('document_added')
+ added = call.result
+ .attachments
+ .reject { |a| current_attachments.include?(a.id) }
- if saved_attachments.present? && Setting.notified_events.include?('document_added')
- users = saved_attachments.first.container.recipients
- users.each do |user|
- UserMailer.attachments_added(user, saved_attachments).deliver_later
- end
+ notify_attachments added
end
+
redirect_to action: 'show', id: @document
end
@@ -118,4 +122,14 @@ class DocumentsController < ApplicationController
def document_params
params.fetch(:document, {}).permit('category_id', 'title', 'description')
end
+
+ def notify_attachments(added)
+ return if added.empty?
+
+ users = added.first.container.recipients
+ users.each do |user|
+ UserMailer.attachments_added(user, added).deliver_later
+ end
+ end
+
end
diff --git a/modules/documents/app/services/documents/create_service.rb b/modules/documents/app/services/documents/create_service.rb
new file mode 100644
index 00000000000..62ba3665e68
--- /dev/null
+++ b/modules/documents/app/services/documents/create_service.rb
@@ -0,0 +1,35 @@
+#-- 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.
+#++
+
+module Documents
+ class CreateService < ::BaseServices::Create
+ include Attachments::ReplaceAttachments
+ end
+end
diff --git a/modules/documents/app/services/documents/set_attributes_service.rb b/modules/documents/app/services/documents/set_attributes_service.rb
new file mode 100644
index 00000000000..c9d4a807f3b
--- /dev/null
+++ b/modules/documents/app/services/documents/set_attributes_service.rb
@@ -0,0 +1,35 @@
+#-- 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.
+#++
+
+module Documents
+ class SetAttributesService < ::BaseServices::SetAttributes
+ include Attachments::SetReplacements
+ end
+end
diff --git a/modules/documents/app/services/documents/update_service.rb b/modules/documents/app/services/documents/update_service.rb
new file mode 100644
index 00000000000..5e7013f28e2
--- /dev/null
+++ b/modules/documents/app/services/documents/update_service.rb
@@ -0,0 +1,35 @@
+#-- 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.
+#++
+
+module Documents
+ class UpdateService < ::BaseServices::Update
+ include Attachments::ReplaceAttachments
+ end
+end
diff --git a/modules/documents/spec/controllers/documents_controller_spec.rb b/modules/documents/spec/controllers/documents_controller_spec.rb
index 280bca44531..20f1e318cee 100644
--- a/modules/documents/spec/controllers/documents_controller_spec.rb
+++ b/modules/documents/spec/controllers/documents_controller_spec.rb
@@ -130,6 +130,7 @@ describe DocumentsController do
end
describe "with attachments" do
+ let(:uncontainered) { FactoryBot.create :attachment, container: nil, author: admin }
before do
notify_project = project
FactoryBot.create(:member, project: notify_project, user: user, roles: [role])
@@ -140,7 +141,7 @@ describe DocumentsController do
document: FactoryBot.attributes_for(:document, title: "New Document",
project_id: notify_project.id,
category_id: default_category.id),
- attachments: { '1' => { description: "sample file", file: file_attachment } }
+ attachments: { '1' => { id: uncontainered.id } }
}
end
@@ -149,8 +150,7 @@ describe DocumentsController do
expect(document.attachments.count).to eql 1
attachment = document.attachments.first
- expect(attachment.description).to eql "sample file"
- expect(attachment.filename).to eql "testfile.txt"
+ expect(uncontainered.reload).to eql attachment
end
it "should redirect to the documents-page" do
@@ -176,12 +176,14 @@ describe DocumentsController do
end
describe '#add_attachment' do
+ let(:uncontainered) { FactoryBot.create :attachment, container: nil, author: admin }
+
before do
document
post :add_attachment,
params: {
id: document.id,
- attachments: { '1' => { description: "sample file", file: file_attachment } }
+ attachments: { '1' => { id: uncontainered.id } }
}
end
@@ -189,6 +191,7 @@ describe DocumentsController do
expect(response).to be_redirect
document.reload
expect(document.attachments.length).to eq(1)
+ expect(uncontainered.reload).to eq document.attachments.first
end
end
diff --git a/modules/job_status/spec/services/documents/create_service_spec.rb b/modules/job_status/spec/services/documents/create_service_spec.rb
new file mode 100644
index 00000000000..7bf9c46ff4a
--- /dev/null
+++ b/modules/job_status/spec/services/documents/create_service_spec.rb
@@ -0,0 +1,6 @@
+require 'spec_helper'
+require 'services/base_services/behaves_like_create_service'
+
+describe Documents::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service'
+end
\ No newline at end of file
diff --git a/modules/job_status/spec/services/documents/update_service_spec.rb b/modules/job_status/spec/services/documents/update_service_spec.rb
new file mode 100644
index 00000000000..511f0b398d3
--- /dev/null
+++ b/modules/job_status/spec/services/documents/update_service_spec.rb
@@ -0,0 +1,36 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+require 'services/base_services/behaves_like_update_service'
+
+describe Documents::UpdateService, type: :model do
+ it_behaves_like 'BaseServices update service'
+end