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

reuse superclasses for copy service

ulferts0a63b0103931
diff --git a/app/services/base_services/copy.rb b/app/services/base_services/copy.rb
index dd9b3907386..605db5c6c03 100644
--- a/app/services/base_services/copy.rb
+++ b/app/services/base_services/copy.rb
@@ -27,7 +27,7 @@
#++
module BaseServices
- class Copy < ::BaseServices::BaseContracted
+ class Copy < ::BaseServices::Write
alias_attribute(:source, :model)
##
@@ -66,27 +66,25 @@ module BaseServices
super
end
- def after_validate(params, _call)
- # Initialize the target resource to copy into
- call = initialize_copy(source, params)
-
+ def persist(call)
# Return only the unsaved copy
return call if params[:attributes_only]
- # Try to save the result or return its errors
- copy_instance = call.result
- unless copy_instance.save
- return ServiceResult.failure(result: copy_instance, errors: copy_instance.errors)
- end
-
- self.class.copy_dependencies.each do |service_cls|
- next if skip_dependency?(params, service_cls)
+ super.tap do |super_call|
+ copy_instance = super_call.result
+ self.class.copy_dependencies.each do |service_cls|
+ next if skip_dependency?(params, service_cls)
- call.merge! call_dependent_service(service_cls, target: copy_instance, params:),
- without_success: true
+ super_call.merge! call_dependent_service(service_cls, target: copy_instance, params:),
+ without_success: true
+ end
end
+ end
- call
+ def after_perform(call)
+ return call if params[:attributes_only]
+
+ super
end
protected
@@ -123,8 +121,8 @@ module BaseServices
.call(params:)
end
- def initialize_copy(source, params)
- raise NotImplementedError
+ def instance(_params)
+ source.class.new
end
def default_contract_class
diff --git a/app/services/grids/copy_service.rb b/app/services/grids/copy_service.rb
index d2f6c9680d3..357707a91ef 100644
--- a/app/services/grids/copy_service.rb
+++ b/app/services/grids/copy_service.rb
@@ -51,14 +51,8 @@ module Grids
protected
- def initialize_copy(source, params)
- grid = source.dup
-
- initialize_new_grid! grid, source, params
-
- ServiceResult.new success: grid.save, result: grid
+ def set_attributes_params(_params)
+ source.dup.attributes
end
-
- def initialize_new_grid!(_new_grid, _original_grid, _params); end
end
end
diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rb
index ff2c5884f20..c9df1355e9a 100644
--- a/app/services/projects/copy_service.rb
+++ b/app/services/projects/copy_service.rb
@@ -56,70 +56,86 @@ module Projects
!Copy::Dependency.should_copy?(params, dependency_cls.identifier.to_sym)
end
- def initialize_copy(source, params)
- target = Project.new
+ def set_attributes_params(_params)
+ attributes = source_attributes.merge(
+ # Clear enabled modules
+ enabled_module_names: source_enabled_modules,
+ types: source_types,
+ work_package_custom_fields: source_custom_fields,
+
+ # Copy status object
+ status: source_status
+ )
- target.attributes = source.attributes.dup.except(*skipped_attributes)
- # Clear enabled modules
- target.enabled_modules = []
- target.enabled_module_names = source.enabled_module_names - %w[repository]
- target.types = source.types
- target.work_package_custom_fields = source.work_package_custom_fields
+ only_allowed_parent_id(attributes)
+ .merge(source_custom_field_attributes)
+ .merge(target_project_params)
+ end
- # Copy status object
- target.status = source.status&.dup
+ def before_perform(params, service_call)
+ super.tap do |super_call|
+ # Retain values after the set attributes service
+ retain_attributes(source, super_call.result)
- # Take over the CF values for attributes
- target.custom_field_values = source.custom_value_attributes
+ # Retain the project in the state for other dependent
+ # copy services to use
+ state.project = super_call.result
+ end
+ end
- # Additional input target params
- target_project_params = params[:target_project_params].with_indifferent_access
+ def contract_options
+ { copy_source: source, validate_model: true }
+ end
- cleanup_target_project_params(source, target, target_project_params)
- cleanup_target_project_attributes(source, target, target_project_params)
+ def retain_attributes(source, target)
+ # Ensure we keep the public value of the source project
+ # which might get overridden by the SetAttributesService
+ # unless the user provided a different value
+ target.public = source.public unless target_project_params.key?(:public)
+ end
- # Assign additional params from user
- call = Projects::SetAttributesService
- .new(user:,
- model: target,
- contract_class: Projects::CopyContract,
- contract_options: { copy_source: source, validate_model: true })
- .with_state(state)
- .call(target_project_params)
+ def skipped_attributes
+ %w[id created_at updated_at name identifier active templated lft rgt]
+ end
- # Retain values after the set attributes service
- retain_attributes(source, target, target_project_params)
+ def source_attributes
+ source.attributes.dup.except(*skipped_attributes).with_indifferent_access
+ end
- # Retain the project in the state for other dependent
- # copy services to use
- state.project = target
+ def source_enabled_modules
+ source.enabled_module_names - %w[repository]
+ end
- call
+ def source_status
+ source.status&.attributes
end
- def retain_attributes(source, target, target_project_params)
- # Ensure we keep the public value of the source project
- # which might get overridden by the SetAttributesService
- # unless the user provided a different value
- target.public = source.public unless target_project_params.key?(:public)
+ def source_types
+ source.types
end
- def cleanup_target_project_params(_source, _target, target_project_params)
- if (parent_id = target_project_params[:parent_id]) && (parent = Project.find_by(id: parent_id)) && !user.allowed_to?(
- :add_subprojects, parent
- )
- target_project_params.delete(:parent_id)
- end
+ def source_custom_fields
+ source.work_package_custom_fields
end
- def cleanup_target_project_attributes(_source, target, _target_project_params)
- if target.parent && !user.allowed_to?(:add_subprojects, target.parent)
- target.parent = nil
- end
+ def source_custom_field_attributes
+ source
+ .custom_value_attributes
+ .transform_keys { |key| "custom_field_#{key}" }
end
- def skipped_attributes
- %w[id created_at updated_at name identifier active templated lft rgt]
+ # Additional input target params
+ def target_project_params
+ params[:target_project_params].with_indifferent_access
+ end
+
+ def only_allowed_parent_id(attributes)
+ if (parent_id = attributes[:parent_id]) && (parent = Project.find_by(id: parent_id)) &&
+ !user.allowed_to?(:add_subprojects, parent)
+ attributes.except(:parent_id)
+ else
+ attributes
+ end
end
end
end
diff --git a/app/services/queries/copy_service.rb b/app/services/queries/copy_service.rb
index 105f3cdb0ed..bef9f193352 100644
--- a/app/services/queries/copy_service.rb
+++ b/app/services/queries/copy_service.rb
@@ -37,16 +37,19 @@ module Queries
protected
- def initialize_copy(source, _params)
- new_query = ::Query.new source.attributes.dup.except(*skipped_attributes)
+ def set_attributes(_params)
+ new_query = copied_query
new_query.sort_criteria = source.sort_criteria if source.sort_criteria
- new_query.project = state.project || source.project
::Queries::Copy::FiltersMapper
.new(state, new_query.filters)
.map_filters!
- ServiceResult.new(success: new_query.save, result: new_query)
+ ServiceResult.new(success: new_query.valid?, result: new_query)
+ end
+
+ def copied_query
+ ::Query.new source.attributes.dup.except(*skipped_attributes).merge(project: state.project || source.project)
end
def skipped_attributes
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index 512891874f0..af5c9ee8438 100644
--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -131,7 +131,7 @@ module Redmine
value = cv.value
hash[key] =
- if existing = hash[key]
+ if (existing = hash[key])
Array(existing) << value
else
value
diff --git a/modules/boards/app/services/boards/copy_service.rb b/modules/boards/app/services/boards/copy_service.rb
index b740e4fa206..6f05f071e0e 100644
--- a/modules/boards/app/services/boards/copy_service.rb
+++ b/modules/boards/app/services/boards/copy_service.rb
@@ -30,8 +30,9 @@ module Boards
class CopyService < ::Grids::CopyService
protected
- def initialize_new_grid!(new_board, original_board, _params)
- new_board.project = state.project || original_board.project
+ def set_attributes_params(params)
+ super
+ .merge(project: state.project || model.project)
end
end
end
diff --git a/modules/boards/app/services/boards/set_attributes_service.rb b/modules/boards/app/services/boards/set_attributes_service.rb
new file mode 100644
index 00000000000..8a7676a2c7f
--- /dev/null
+++ b/modules/boards/app/services/boards/set_attributes_service.rb
@@ -0,0 +1,29 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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 COPYRIGHT and LICENSE files for more details.
+
+module Boards
+ class SetAttributesService < ::BaseServices::SetAttributes; end
+end
diff --git a/modules/overviews/app/services/overviews/copy_service.rb b/modules/overviews/app/services/overviews/copy_service.rb
index dfc9152e525..6d5f10b8483 100644
--- a/modules/overviews/app/services/overviews/copy_service.rb
+++ b/modules/overviews/app/services/overviews/copy_service.rb
@@ -30,12 +30,13 @@ module Overviews
class CopyService < ::Grids::CopyService
protected
- def initialize_new_grid!(new_overview, _original_overview, _params)
+ def set_attributes_params(params)
unless state.project
raise ArgumentError, "Overviews can only be copied as part of a project. Each project may only contain 1 overview itself."
end
- new_overview.project = state.project
+ super
+ .merge(project: state.project)
end
end
end
diff --git a/modules/overviews/app/services/overviews/set_attributes_service.rb b/modules/overviews/app/services/overviews/set_attributes_service.rb
new file mode 100644
index 00000000000..0cfd3ae61b1
--- /dev/null
+++ b/modules/overviews/app/services/overviews/set_attributes_service.rb
@@ -0,0 +1,29 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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 COPYRIGHT and LICENSE files for more details.
+
+module Overviews
+ class SetAttributesService < ::BaseServices::SetAttributes; end
+end
diff --git a/spec/workers/copy_project_job_spec.rb b/spec/workers/copy_project_job_spec.rb
index c4bddcd2e70..9686c863b3d 100644
--- a/spec/workers/copy_project_job_spec.rb
+++ b/spec/workers/copy_project_job_spec.rb
@@ -45,7 +45,7 @@ describe CopyProjectJob, type: :model do
let(:target_project) { create(:project) }
let(:copy_job) do
- CopyProjectJob.new
+ described_class.new
end
it 'sets locale correctly' do
@@ -126,7 +126,7 @@ describe CopyProjectJob, type: :model do
end
let(:copy_job) do
- CopyProjectJob.new.tap do |job|
+ described_class.new.tap do |job|
job.perform user_id: admin.id,
source_project_id: source_project.id,
target_project_params: params,
@@ -155,7 +155,7 @@ describe CopyProjectJob, type: :model do
let(:admin) { create(:admin) }
let(:source_project) { create(:project) }
let(:copy_job) do
- CopyProjectJob.new.tap do |job|
+ described_class.new.tap do |job|
job.perform user_id: admin.id,
source_project_id: source_project.id,
target_project_params: params,
@@ -188,7 +188,7 @@ describe CopyProjectJob, type: :model do
shared_context 'copy project' do
before do
- CopyProjectJob.new.tap do |job|
+ described_class.new.tap do |job|
job.perform user_id: user.id,
source_project_id: project_to_copy.id,
target_project_params: params,
@@ -204,7 +204,7 @@ describe CopyProjectJob, type: :model do
end
describe 'subproject' do
- let(:params) { { name: 'Copy', identifier: 'copy', parent_id: project.id } }
+ let(:params) { { name: 'Copy', identifier: 'copy' } }
let(:subproject) do
create(:project, parent: project).tap do |p|
create(:member,
@@ -232,7 +232,7 @@ describe CopyProjectJob, type: :model do
perform_enqueued_jobs
mail = ActionMailer::Base.deliveries
- .find { |m| m.message_id.start_with? "op.project-#{subject.id}" }
+ .find { |m| m.message_id.start_with? "op.project-#{subject.id}" }
expect(mail).to be_present
expect(mail.subject).to eq "Created project #{subject.name}"
@@ -240,6 +240,27 @@ describe CopyProjectJob, type: :model do
end
end
+ describe 'user without add_subprojects permission in parent and when explicitly setting that parent' do
+ let(:params) { { name: 'Copy', identifier: 'copy', parent_id: project.id } }
+
+ include_context 'copy project' do
+ let(:project_to_copy) { subproject }
+ end
+
+ it 'does not copy the project' do
+ expect(subject).to be_nil
+ end
+
+ it "notifies the user of that parent not being allowed" do
+ perform_enqueued_jobs
+
+ mail = ActionMailer::Base.deliveries.first
+ expect(mail).to be_present
+ expect(mail.subject).to eq I18n.t('copy_project.failed', source_project_name: subproject.name)
+ expect(mail.to).to eq [user.mail]
+ end
+ end
+
describe 'user with add_subprojects permission in parent' do
let(:role_add_subproject) { create(:role, permissions: [:add_subprojects]) }
let(:member_add_subproject) do