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
merge changed_by_system into contract options
ulferts4eab6effe12b
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rbindex 127ac6afc21..1550fd3fea9 100644--- a/app/contracts/model_contract.rb+++ b/app/contracts/model_contract.rb@@ -189,8 +189,8 @@ class ModelContract < Reform::Contractdef attributes_changed_by_userchanged = model.changed- if model.respond_to?(:changed_by_system)- changed -= model.changed_by_system+ if options[:changed_by_system]+ changed -= options[:changed_by_system]endchangeddiff --git a/app/models/mixins/changed_by_system.rb b/app/models/mixins/changed_by_system.rbdeleted file mode 100644index 110ca432ce5..00000000000--- a/app/models/mixins/changed_by_system.rb+++ /dev/null@@ -1,65 +0,0 @@-#-- encoding: UTF-8--#-- copyright-# OpenProject is a project management system.-# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)-#-# 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 doc/COPYRIGHT.rdoc for more details.-#++--module Mixins- module ChangedBySystem- extend ActiveSupport::Concern-- def changed_by_system(attributes = nil)- @changed_by_system ||= []-- if attributes- @changed_by_system += Array(attributes)- end-- @changed_by_system- end-- def change_by_system- prior_changes = non_no_op_changes-- ret = yield-- changed_by_system(changed_compared_to(prior_changes))-- ret- end-- private-- def non_no_op_changes- changes.reject { |_, (old, new)| old == 0 && new.nil? }- end-- def changed_compared_to(prior_changes)- changed.select { |c| !prior_changes[c] || prior_changes[c].last != changes[c].last }- end- end-enddiff --git a/app/services/base_services/set_attributes.rb b/app/services/base_services/set_attributes.rbindex 2113bb696a1..35507b601e8 100644--- a/app/services/base_services/set_attributes.rb+++ b/app/services/base_services/set_attributes.rb@@ -36,17 +36,11 @@ module BaseServicesself.user = userself.model = model- # Allow tracking changes caused by a user but done for him by the system.- # E.g. fixed_version of a work package might need to be changed as the user changed the project.- # This is currently used for permission checks where the changed project is checked but the fixed_version- # is not if it is done by the system.- model.extend(Mixins::ChangedBySystem)-self.contract_class = contract_classself.contract_options = contract_optionsend- def call(params, contract_options: {})+ def call(params)set_attributes(params)validate_and_resultdiff --git a/app/services/concerns/contracted.rb b/app/services/concerns/contracted.rbindex c5f5fb1064d..ea46a9eed68 100644--- a/app/services/concerns/contracted.rb+++ b/app/services/concerns/contracted.rb@@ -43,10 +43,30 @@ module Concerns::Contracted@contract_class = clsend+ def changed_by_system(attributes = nil)+ @changed_by_system ||= []++ if attributes+ @changed_by_system += Array(attributes)+ end++ @changed_by_system+ end++ def change_by_system+ prior_changes = non_no_op_changes++ ret = yield++ changed_by_system(changed_compared_to(prior_changes))++ ret+ end+privatedef instantiate_contract(object, user, options: {})- contract_class.new(object, user, options: options)+ contract_class.new(object, user, options: { changed_by_system: changed_by_system }.merge(options))enddef validate_and_save(object, user, options: {})@@ -77,5 +97,13 @@ module Concerns::Contractedtrueendend++ def non_no_op_changes+ model.changes.reject { |_, (old, new)| old == 0 && new.nil? }+ end++ def changed_compared_to(prior_changes)+ model.changed.select { |c| !prior_changes[c] || prior_changes[c].last != model.changes[c].last }+ endendenddiff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 1d15ae076d9..2b9990cfa18 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -37,14 +37,14 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesset_attachments_attributes(attributes)set_static_attributes(attributes)- work_package.change_by_system do+ change_by_system doset_default_attributes(attributes)update_project_dependent_attributesendset_custom_attributes(attributes)- work_package.change_by_system do+ change_by_system doupdate_datesreassign_invalid_status_if_type_changedset_templated_description@@ -130,7 +130,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesdef update_project_dependent_attributesreturn unless work_package.project_id_changed? && work_package.project_id- work_package.change_by_system do+ change_by_system doset_fixed_version_to_nilreassign_categorydiff --git a/spec/contracts/work_packages/create_contract_spec.rb b/spec/contracts/work_packages/create_contract_spec.rbindex a7fcbc72dcc..5bdb709bd71 100644--- a/spec/contracts/work_packages/create_contract_spec.rb+++ b/spec/contracts/work_packages/create_contract_spec.rb@@ -119,21 +119,23 @@ describe WorkPackages::CreateContract dowork_package.project = projectend- it 'is valid if the user is set by the sytem and the user is the user the contract is evaluated for' do- work_package.extend(Mixins::ChangedBySystem)+ context 'if the user is set by the sytem and the user is the user the contract is evaluated for' do+ subject(:contract) { described_class.new(work_package, user, options: { changed_by_system: ['author_id'] }) }- work_package.change_by_system do+ it 'is valid' dowork_package.author = user- end- expect(validated_contract.errors[:author_id]).to be_empty+ expect(validated_contract.errors[:author_id]).to be_empty+ endend- it 'is invalid if the user is different from the user the contract is evaluated for' do- work_package.author = FactoryBot.build_stubbed(:user)+ context 'if the user is different from the user the contract is evaluated for' do+ it 'is invalid' do+ work_package.author = FactoryBot.build_stubbed(:user)- expect(validated_contract.errors.symbols_for(:author_id))- .to match_array %i[invalid error_readonly]+ expect(validated_contract.errors.symbols_for(:author_id))+ .to match_array %i[invalid error_readonly]+ endendendenddiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 9200e380285..53f88264d48 100644--- a/spec/services/work_packages/set_attributes_service_spec.rb+++ b/spec/services/work_packages/set_attributes_service_spec.rb@@ -224,7 +224,7 @@ describe WorkPackages::SetAttributesService, type: :model doit 'notes the author to be system changed' dosubject- expect(work_package.changed_by_system)+ expect(instance.changed_by_system).to include('author_id')endend@@ -529,7 +529,7 @@ describe WorkPackages::SetAttributesService, type: :model doit 'adds change to system changes' dosubject- expect(work_package.changed_by_system)+ expect(instance.changed_by_system).to include('category_id')endend@@ -558,7 +558,7 @@ describe WorkPackages::SetAttributesService, type: :model doit 'adds change to system changes' dosubject- expect(work_package.changed_by_system)+ expect(instance.changed_by_system).to include('type_id')endend@@ -576,7 +576,7 @@ describe WorkPackages::SetAttributesService, type: :model doit 'adds change to system changes' dosubject- expect(work_package.changed_by_system)+ expect(instance.changed_by_system).to include('type_id')endend@@ -594,7 +594,7 @@ describe WorkPackages::SetAttributesService, type: :model doit 'does not set the change to system changes' dosubject- expect(work_package.changed_by_system)+ expect(instance.changed_by_system).not_to include('type_id')endend