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
Refactor copy services to use shared state
Oliver Günther01954c26bc6c
diff --git a/app/contracts/empty_contract.rb b/app/contracts/empty_contract.rbindex ecc2137d7e1..39c06446f7f 100644--- a/app/contracts/empty_contract.rb+++ b/app/contracts/empty_contract.rb@@ -28,7 +28,7 @@# See docs/COPYRIGHT.rdoc for more details.#++-class EmptyContract+class EmptyContract < ModelContractdef initialize(*); enddef validatediff --git a/app/contracts/projects/copy_contract.rb b/app/contracts/projects/copy_contract.rbindex a008e78c1b2..35c6ec1be02 100644--- a/app/contracts/projects/copy_contract.rb+++ b/app/contracts/projects/copy_contract.rb@@ -37,7 +37,7 @@ module Projectsprivatedef validate_user_allowed_to_manage- errors.add :base, :error_unauthorized unless user.allowed_to?(:copy_projects, options[:copied_from])+ errors.add :base, :error_unauthorized unless user.allowed_to?(:copy_projects, options[:copy_source])endendenddiff --git a/app/contracts/queries/copy_contract.rb b/app/contracts/queries/copy_contract.rbnew file mode 100644index 00000000000..17c2605d411--- /dev/null+++ b/app/contracts/queries/copy_contract.rb@@ -0,0 +1,35 @@+#-- 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.+#++++require 'queries/base_contract'++module Queries+ class CopyContract < BaseContract+ end+enddiff --git a/app/controllers/work_packages/moves_controller.rb b/app/controllers/work_packages/moves_controller.rbindex b66d0862846..6cd9b51ad58 100644--- a/app/controllers/work_packages/moves_controller.rb+++ b/app/controllers/work_packages/moves_controller.rb@@ -100,7 +100,7 @@ class WorkPackages::MovesController < ApplicationControllerdef dependent_error_ids(parent_id, service_call)ids = service_call.results_with_errors(include_self: false)- .map { |result| result.context[:copied_from]&.id }+ .map { |result| result.state.copied_from_work_package_id }.compactif ids.present?diff --git a/app/services/base_services/base_callable.rb b/app/services/base_services/base_callable.rbnew file mode 100644index 00000000000..f3c35bbaa7e--- /dev/null+++ b/app/services/base_services/base_callable.rb@@ -0,0 +1,51 @@+#-- 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 BaseServices+ class BaseCallable+ extend ActiveModel::Callbacks+ define_model_callbacks :call++ include ::Shared::ServiceContext+ include ::WithReversibleState++ def call(*args)+ run_callbacks(:call) do+ perform(*args)+ end+ end++ protected++ def perform(*args)+ raise NotImplementedError+ end+ end+enddiff --git a/app/services/base_services/base_contracted.rb b/app/services/base_services/base_contracted.rbindex fc9836ac802..6d7fc4347ee 100644--- a/app/services/base_services/base_contracted.rb+++ b/app/services/base_services/base_contracted.rb@@ -29,7 +29,7 @@#++module BaseServices- class BaseContracted+ class BaseContracted < BaseCallableinclude Contractedinclude Shared::ServiceContext@@ -41,23 +41,19 @@ module BaseServicesself.contract_options = contract_optionsend- def call(params = nil)- in_context(model, true) do- perform(params)- end- end-protecteddef perform(params)- service_call = before_perform(params)+ in_context(model, true) do+ service_call = before_perform(params)- service_call = validate_contract(service_call) if service_call.success?- service_call = after_validate(params, service_call) if service_call.success?- service_call = persist(service_call) if service_call.success?- service_call = after_perform(service_call) if service_call.success?+ service_call = validate_contract(service_call) if service_call.success?+ service_call = after_validate(params, service_call) if service_call.success?+ service_call = persist(service_call) if service_call.success?+ service_call = after_perform(service_call) if service_call.success?- service_call+ service_call+ endenddef before_perform(_params)diff --git a/app/services/base_services/copy.rb b/app/services/base_services/copy.rbindex 7ec96134346..2eb0fccacbf 100644--- a/app/services/base_services/copy.rb+++ b/app/services/base_services/copy.rb@@ -34,13 +34,14 @@ module BaseServices# BaseContracted needs a `model` attributealias_attribute :model, :source- def initialize(user:, source:, contract_class: nil, contract_options: { copied_from: source })+ def initialize(user:, source:, contract_class: nil, contract_options: { copy_source: source })@source = sourcesuper(user: user, contract_class: contract_class, contract_options: contract_options)enddef call(params)User.execute_as(user) do+ prepare(params)perform(params)endend@@ -52,11 +53,10 @@ module BaseServices# Return only the unsaved copyreturn call if params[:attributes_only]- # Allow to keep a state object between services- state = {}-copy_dependencies.each do |service_cls|- call.merge! call_dependent_service(service_cls, target: call.result, params: params, state: state)+ next if skip_dependency?(params, service_cls)++ call.merge! call_dependent_service(service_cls, target: call.result, params: params)endcall@@ -64,18 +64,35 @@ module BaseServicesprotected+ ##+ # Should the dependency be skipped for this service run?+ def skip_dependency?(_params, _dependency_cls)+ false+ end++ ##+ # Sets up a state object that gets+ # passed around to all service calls from here+ #+ # Note that for dependent copy services to be called+ # this will already be present.+ def prepare(params)+ params[:copy_state] ||= {}+ end+### dependent services to copy associationsdef copy_dependencies- raise NotImplementedError+ []end### Calls a dependent service with the source and copy instance- def call_dependent_service(service_cls, target:, params:, state:)+ def call_dependent_service(service_cls, target:, params:)service_cls- .new(source: model, target: target, user: user)- .call(params: params, state: state)+ .new(source: source, target: target, user: user)+ .with_state(state)+ .call(params: params)enddef initialize_copy(source, params)diff --git a/app/services/concerns/with_reversible_state.rb b/app/services/concerns/with_reversible_state.rbnew file mode 100644index 00000000000..44f2b57b76c--- /dev/null+++ b/app/services/concerns/with_reversible_state.rb@@ -0,0 +1,66 @@+#-- 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 WithReversibleState+ extend ActiveSupport::Concern++ included do+ attr_reader :state+ around_call :assign_state++ ##+ # Reuse or append state to the service+ def with_state(state = {})+ @state = ::Shared::ServiceState.build(state)+ self+ end++ ##+ # Access to the shared service state+ def state+ @state ||= ::Shared::ServiceState.build+ end++ ##+ # Rollback changes made+ def rollback+ # Nothing to do by default+ end++ ##+ # Assign state to the service result+ def assign_state+ yield.tap do |call|+ state.called!(self)+ call.state = state+ end+ end+ end+enddiff --git a/app/services/copy/dependency.rb b/app/services/copy/dependency.rbindex f4188de82fb..14755ce39b0 100644--- a/app/services/copy/dependency.rb+++ b/app/services/copy/dependency.rb@@ -31,13 +31,12 @@### Dependent service to be executed under the BaseServices::Copy servicemodule Copy- class Dependency+ class Dependency < ::BaseServices::BaseCallableattr_reader :source,:target,:user,:result-### Identifier of this dependency to include/excludedef self.identifier@@ -53,21 +52,6 @@ module Copy@result = ServiceResult.new(result: target, success: true, errors: ActiveModel::Errors.new(target))end- def call(params:, state:)- return result if skip?(params)-- begin- perform(params: params, state: state)- rescue StandardError => e- Rails.logger.error { "Failed to copy dependency #{self.class.name}: #{e.message}" }- result.success = false- result.errors.add(self.class.identifier, :could_not_be_copied)- end-- result- end--protected##@@ -76,22 +60,19 @@ module Copyresult.errors.add(:base, "#{model.class.model_name.human} '#{model}': #{errors.full_messages.join(". ")}")end- ##- # Whether this entire dependency should be skipped- def skip?(params)- skip_dependency?(params, self.class.identifier)- end-- ##- # Whether to skip the given key.- # Useful when copying nested dependencies- def skip_dependency?(params, name)- return false unless params[:only].present?+ def perform(params:)+ begin+ copy_dependency(params: params)+ rescue StandardError => e+ Rails.logger.error { "Failed to copy dependency #{self.class.name}: #{e.message}" }+ result.success = false+ result.errors.add(self.class.identifier, :could_not_be_copied)+ end- !params[:only].any? { |key| key.to_s == name.to_s }+ resultend- def perform(params:, state:)+ def copy_dependency(params:)raise NotImplementedErrorendenddiff --git a/app/services/projects/copy/boards_dependent_service.rb b/app/services/projects/copy/boards_dependent_service.rbnew file mode 100644index 00000000000..0ce361c2190--- /dev/null+++ b/app/services/projects/copy/boards_dependent_service.rb@@ -0,0 +1,54 @@+#-- 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 Projects::Copy+ class BoardsDependentService < ::Copy::Dependency+ protected++ # Copies boards from +project++ # Only includes the queries visible in the wp table view.+ def copy_dependency(params)+ ::Boards::Grid.where(project: source).find_each do |board|+ duplicate_board(board, params)+ end+ end++ def duplicate_board(board, params)+ ::Boards::CopyService+ .new(source: board, user: user)+ .with_state(state)+ .call(params.merge)+ .on_failure do |result|++ add_error! board, result.errors+ end+ end+ end+enddiff --git a/app/services/projects/copy/categories_dependent_service.rb b/app/services/projects/copy/categories_dependent_service.rbindex 1888ba78ba5..0925def1abe 100644--- a/app/services/projects/copy/categories_dependent_service.rb+++ b/app/services/projects/copy/categories_dependent_service.rb@@ -32,7 +32,7 @@ module Projects::Copyclass CategoriesDependentService < ::Copy::Dependencyprotected- def perform(params:, state:)+ def copy_dependency(params:)source.categories.find_each do |category|new_category = Category.newnew_category.send(:assign_attributes, category.attributes.dup.except('id', 'project_id'))diff --git a/app/services/projects/copy/forums_dependent_service.rb b/app/services/projects/copy/forums_dependent_service.rbindex aebe2e108ae..700fe381ce9 100644--- a/app/services/projects/copy/forums_dependent_service.rb+++ b/app/services/projects/copy/forums_dependent_service.rb@@ -32,7 +32,7 @@ module Projects::Copyclass ForumsDependentService < ::Copy::Dependencyprotected- def perform(params:, state:)+ def copy_dependency(params:)source.forums.find_each do |forum|new_forum = Forum.newnew_forum.attributes = forum.attributes.dup.except('id',diff --git a/app/services/projects/copy/members_dependent_service.rb b/app/services/projects/copy/members_dependent_service.rbindex be7f0dc9e27..18eae442ba6 100644--- a/app/services/projects/copy/members_dependent_service.rb+++ b/app/services/projects/copy/members_dependent_service.rb@@ -32,7 +32,7 @@ module Projects::Copyclass MembersDependentService < ::Copy::Dependencyprotected- def perform(params:, state:)+ def copy_dependency(*)# Copy users first, then groups to handle members with inherited and given rolesmembers_to_copy = []members_to_copy += source.memberships.select { |m| m.principal.is_a?(User) }diff --git a/app/services/projects/copy/queries_dependent_service.rb b/app/services/projects/copy/queries_dependent_service.rbindex 9d38971e208..a93f9f5929d 100644--- a/app/services/projects/copy/queries_dependent_service.rb+++ b/app/services/projects/copy/queries_dependent_service.rb@@ -32,53 +32,22 @@ module Projects::Copyclass QueriesDependentService < ::Copy::Dependencyprotected- def perform(params:, state:)- copy_queries(state[:work_packages_map])- end-# Copies queries from +project+# Only includes the queries visible in the wp table view.- def copy_queries(work_packages_map)-+ def copy_dependency(params:)source.queries.non_hidden.includes(:query_menu_item).each do |query|- new_query = duplicate_query(query)- duplicate_query_menu_item(query, new_query)- duplicate_query_order(query, new_query, work_packages_map) if query.manually_sorted?+ duplicate_query(query, params)endend- def duplicate_query(query)- new_query = ::Query.new name: '_'- new_query.attributes = query.attributes.dup.except('id', 'project_id', 'sort_criteria')- new_query.sort_criteria = query.sort_criteria if query.sort_criteria- new_query.set_context- new_query.project = target- target.queries << new_query- new_query.set_context-- new_query- end-- def duplicate_query_order(query, new_query, work_packages_map)- query.ordered_work_packages.find_each do |ordered_wp|- wp_id = work_packages_map[ordered_wp.work_package_id]- # Nothing to do if the work package could not be copied for whatever reason- next unless wp_id-- copied = ordered_wp.dup- copied.query_id = new_query.id- copied.work_package_id = wp_id- copied.save- end- end+ def duplicate_query(query, params)+ ::Queries::CopyService+ .new(source: query, user: user)+ .with_state(state)+ .call(params.merge)+ .on_failure do |result|- def duplicate_query_menu_item(query, new_query)- if query.query_menu_item && new_query.persisted?- ::MenuItems::QueryMenuItem.create(- navigatable_id: new_query.id,- name: SecureRandom.uuid,- title: query.query_menu_item.title- )+ add_error! query, result.errorsendendenddiff --git a/app/services/projects/copy/versions_dependent_service.rb b/app/services/projects/copy/versions_dependent_service.rbindex 9ff8667652f..3e990ed986d 100644--- a/app/services/projects/copy/versions_dependent_service.rb+++ b/app/services/projects/copy/versions_dependent_service.rb@@ -32,7 +32,7 @@ module Projects::Copyclass VersionsDependentService < ::Copy::Dependencyprotected- def perform(params:, state:)+ def copy_dependency(params:)source.versions.each do |version|new_version = Version.newnew_version.attributes = version.attributes.dup.except('id', 'project_id', 'created_on', 'updated_at')diff --git a/app/services/projects/copy/wiki_dependent_service.rb b/app/services/projects/copy/wiki_dependent_service.rbindex bd3a9757e02..4d1eaeb5d80 100644--- a/app/services/projects/copy/wiki_dependent_service.rb+++ b/app/services/projects/copy/wiki_dependent_service.rb@@ -34,7 +34,7 @@ module Projects::Copyprotected- def perform(params:, state:)+ def copy_dependency(params:)# Check that the source project has a wiki firstreturn if source.wiki.nil?diff --git a/app/services/projects/copy/work_packages_dependent_service.rb b/app/services/projects/copy/work_packages_dependent_service.rbindex efc882fd2dd..642d6ceab5c 100644--- a/app/services/projects/copy/work_packages_dependent_service.rb+++ b/app/services/projects/copy/work_packages_dependent_service.rb@@ -34,7 +34,7 @@ module Projects::Copyprotected- def perform(params:, state:)+ def copy_dependency(params:)# Stores the source work_package id as a key and the copied work package ID as the# value. Used to map the two together for work_package relations.work_packages_map = {}@@ -63,14 +63,14 @@ module Projects::Copynext unless new_wp_id# Attachments- unless skip_dependency?(params, :work_package_attachments)+ unless params[:only].present? && !params[:only].include?(:work_package_attachments)copy_attachments(wp.id, new_wp_id, 'WorkPackage')endcopy_relations(wp, new_wp_id, work_packages_map)end- state[:work_packages_map] = work_packages_map+ state.work_package_id_lookup = work_packages_mapenddef copy_work_package(source_work_package, parent_id, user_cf_ids)diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rbindex 0f821d7b295..29c9834008d 100644--- a/app/services/projects/copy_service.rb+++ b/app/services/projects/copy_service.rb@@ -30,8 +30,25 @@module Projectsclass CopyService < ::BaseServices::Copy++ ##+ # In case a rollback is needed,+ # destroy the copied project again.+ def rollback+ state.project&.destroy+ end+protected+ ##+ # Whether to skip the given key.+ # Useful when copying nested dependencies+ def skip_dependency?(params, dependency_cls)+ return false unless params[:only].present?++ params[:only].none? { |key| key.to_s == dependency_cls.identifier.to_s }+ end+def copy_dependencies[::Projects::Copy::MembersDependentService,@@ -40,7 +57,8 @@ module Projects::Projects::Copy::WorkPackagesDependentService,::Projects::Copy::WikiDependentService,::Projects::Copy::ForumsDependentService,- ::Projects::Copy::QueriesDependentService+ ::Projects::Copy::QueriesDependentService,+ ::Projects::Copy::BoardsDependentService]end@@ -68,12 +86,16 @@ module Projects.new(user: user,model: target,contract_class: Projects::CopyContract,- contract_options: { copied_from: source })+ contract_options: { copy_source: source }).call(target_project_params)# Retain values after the set attributes serviceretain_attributes(source, target, target_project_params)+ # Retain the project in the state for other dependent+ # copy services to use+ state.project = target+callenddiff --git a/app/services/queries/copy/menu_item_dependent_service.rb b/app/services/queries/copy/menu_item_dependent_service.rbnew file mode 100644index 00000000000..c11e200aa72--- /dev/null+++ b/app/services/queries/copy/menu_item_dependent_service.rb@@ -0,0 +1,49 @@+#-- 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 Queries::Copy+ class MenuItemDependentService < ::Copy::Dependency+ protected++ def copy_dependency(params:)+ duplicate_query_menu_item(source, target)+ end++ def duplicate_query_menu_item(query, new_query)+ if query.query_menu_item && new_query.persisted?+ ::MenuItems::QueryMenuItem.create(+ navigatable_id: new_query.id,+ name: SecureRandom.uuid,+ title: query.query_menu_item.title+ )+ end+ end+ end+enddiff --git a/app/services/queries/copy/ordered_work_packages_dependent_service.rb b/app/services/queries/copy/ordered_work_packages_dependent_service.rbnew file mode 100644index 00000000000..7e101b3c508--- /dev/null+++ b/app/services/queries/copy/ordered_work_packages_dependent_service.rb@@ -0,0 +1,65 @@+#-- 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 Queries::Copy+ class OrderedWorkPackagesDependentService < ::Copy::Dependency+ protected++ def copy_dependency(params:)+ return unless source.manually_sorted?++ duplicate_query_order(source, target)+ end++ def duplicate_query_order(query, new_query)+ query.ordered_work_packages.find_each do |ordered_wp|+ wp_id = lookup_work_package_id(ordered_wp.work_package_id)+ # Nothing to do if the work package could not be copied for whatever reason+ next unless wp_id++ copied = ordered_wp.dup+ copied.query_id = new_query.id+ copied.work_package_id = wp_id+ copied.save+ end+ end++ ##+ # Tries to lookup the work package id if+ # we're in a mapped condition (e.g., copying a project)+ def lookup_work_package_id(id)+ if state.work_package_id_lookup+ state.work_package_id_lookup[id]+ else+ id+ end+ end+ end+enddiff --git a/app/services/queries/copy_service.rb b/app/services/queries/copy_service.rbnew file mode 100644index 00000000000..6e78010f37a--- /dev/null+++ b/app/services/queries/copy_service.rb@@ -0,0 +1,56 @@+#-- 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 Queries+ class CopyService < ::BaseServices::Copy+ protected++ def copy_dependencies+ [+ ::Queries::Copy::MenuItemDependentService,+ ::Queries::Copy::OrderedWorkPackagesDependentService+ ]+ end++ def initialize_copy(source, params)+ new_query = ::Query.new name: '_'+ new_query.attributes = source.attributes.dup.except(*skipped_attributes)+ new_query.sort_criteria = source.sort_criteria if source.sort_criteria+ new_query.project = state.project || source.project+ new_query.set_context++ ServiceResult.new(success: new_query.save, result: new_query)+ end++ def skipped_attributes+ %w[id created_at updated_at project_id sort_criteria]+ end+ end+enddiff --git a/app/services/service_result.rb b/app/services/service_result.rbindex 91b73f82d6b..97b7ef28ec6 100644--- a/app/services/service_result.rb+++ b/app/services/service_result.rb@@ -33,19 +33,19 @@ class ServiceResult:result,:errors,:message_type,- :context,+ :state,:dependent_resultsdef initialize(success: false,errors: nil,message: nil,message_type: nil,- context: {},+ state: ::Shared::ServiceState.new,dependent_results: [],result: nil)self.success = successself.result = result- self.context = context+ self.state = stateinitialize_errors(errors)@message = message@@ -65,6 +65,12 @@ class ServiceResultmerge_dependent!(other)end+ ##+ # Rollback the state if possible+ def rollback!+ state.rollback!+ end+### Print messages to flashdef apply_flash_message!(flash)diff --git a/app/services/shared/service_state.rb b/app/services/shared/service_state.rbnew file mode 100644index 00000000000..05170cf9157--- /dev/null+++ b/app/services/shared/service_state.rb@@ -0,0 +1,69 @@+#-- 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.+#+++require "ostruct"++##+# Service state object to be passed around services+# for remembering state between service calls (e.g., when copying).+#+# Borrows heavily from interactor gem's context class at+# https://github.com/collectiveidea/interactor+module Shared+ class ServiceState < OpenStruct+ ##+ # Builds the context object unless+ # it's already an instance of this context.+ def self.build(state = {})+ self === state ? state : new(state)+ end++ ##+ # Remember that the state was passed to the given service+ def called!(service)+ _called << service+ end++ # Roll back the context on all used services+ def rollback!+ return false if @rolled_back++ _called.reverse_each do |service|+ Rails.logger.debug { "[Service state] Rolling back execution of #{service}." }+ service.rollback+ end+ @rolled_back = true+ end++ # Remembered service calls this context was used against+ def _called+ @_called ||= []+ end+ end+enddiff --git a/app/services/work_packages/copy_service.rb b/app/services/work_packages/copy_service.rbindex 4f81a0213c2..ae07b0389b2 100644--- a/app/services/work_packages/copy_service.rb+++ b/app/services/work_packages/copy_service.rb@@ -59,7 +59,7 @@ class WorkPackages::CopyServicecopy_watchers(copied.result)end- copied.context = { copied_from: work_package }+ copied.state.copied_from_work_package_id = work_package&.idcopiedenddiff --git a/app/views/copy_projects/copy_settings/_copy_associations.html.erb b/app/views/copy_projects/copy_settings/_copy_associations.html.erbindex e2cb2a55416..70b83436b65 100644--- a/app/views/copy_projects/copy_settings/_copy_associations.html.erb+++ b/app/views/copy_projects/copy_settings/_copy_associations.html.erb@@ -33,6 +33,9 @@ See docs/COPYRIGHT.rdoc for more details.<%= render partial: "copy_projects/copy_settings/block_checkbox",locals: { name: "queries", checked: true, label: l(:label_query_plural),count: project.queries.count } %>+ <%= render partial: "copy_projects/copy_settings/block_checkbox",+ locals: { name: "boards", checked: true, label: l('boards.label_boards'),+ count: ::Boards::Grid.where(project: project).count } %><%= render partial: "copy_projects/copy_settings/block_checkbox",locals: { name: "forums", checked: false, label: l(:label_forum_plural),count: project.forums.count } %>diff --git a/modules/boards/app/services/boards/copy/widgets_dependent_service.rb b/modules/boards/app/services/boards/copy/widgets_dependent_service.rbnew file mode 100644index 00000000000..6776d4adabf--- /dev/null+++ b/modules/boards/app/services/boards/copy/widgets_dependent_service.rb@@ -0,0 +1,73 @@+#-- 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 Boards::Copy+ class WidgetsDependentService < ::Copy::Dependency+ protected++ def copy_dependency(params:)+ copy_widgets(source, target, params)+ end++ def copy_widgets(board, new_board, params)+ board.widgets.find_each do |widget|+ unless widget.identifier == 'work_package_query'+ raise "Expected widget work_package_query, got #{widget.identifier}"+ end++ new_widget = duplicate_widget(widget, new_board, params)++ if new_widget && !new_widget.save+ add_error!(new_widget, new_widget.errors)+ end+ end+ end++ def duplicate_widget(widget, new_board, params)+ new_widget = widget.dup+ new_widget.grid = new_board++ query = Query.find widget.options['queryId']++ call = ::Queries::CopyService+ .new(user: user, source: query)+ .with_state(state)+ .call(params)++ if call.success?+ new_widget.options['queryId'] = call.result.id.to_s+ new_widget+ else+ add_error! widget, call.errors+ nil+ end+ end+ end+enddiff --git a/modules/boards/app/services/boards/copy_service.rb b/modules/boards/app/services/boards/copy_service.rbnew file mode 100644index 00000000000..c5d59f05160--- /dev/null+++ b/modules/boards/app/services/boards/copy_service.rb@@ -0,0 +1,58 @@+#-- 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 Boards+ class CopyService < ::BaseServices::Copy++ def initialize(user:, source:)+ super(user: user, source: source, contract_class: ::EmptyContract)+ end++ protected++ def copy_dependencies+ [+ ::Boards::Copy::WidgetsDependentService+ ]+ end++ def initialize_copy(source, params)+ new_board = ::Boards::Grid.new+ new_board.attributes = source.attributes.dup.except(*skipped_attributes)+ new_board.project = state.project || source.project++ ServiceResult.new(success: new_board.save, result: new_board)+ end++ def skipped_attributes+ %w[id created_at updated_at project_id sort_criteria]+ end+ end+end