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

merge changed_by_system into contract options

ulferts4eab6effe12b
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index 127ac6afc21..1550fd3fea9 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -189,8 +189,8 @@ class ModelContract < Reform::Contract
def attributes_changed_by_user
changed = model.changed
- if model.respond_to?(:changed_by_system)
- changed -= model.changed_by_system
+ if options[:changed_by_system]
+ changed -= options[:changed_by_system]
end
changed
diff --git a/app/models/mixins/changed_by_system.rb b/app/models/mixins/changed_by_system.rb
deleted file mode 100644
index 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
-end
diff --git a/app/services/base_services/set_attributes.rb b/app/services/base_services/set_attributes.rb
index 2113bb696a1..35507b601e8 100644
--- a/app/services/base_services/set_attributes.rb
+++ b/app/services/base_services/set_attributes.rb
@@ -36,17 +36,11 @@ module BaseServices
self.user = user
self.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_class
self.contract_options = contract_options
end
- def call(params, contract_options: {})
+ def call(params)
set_attributes(params)
validate_and_result
diff --git a/app/services/concerns/contracted.rb b/app/services/concerns/contracted.rb
index c5f5fb1064d..ea46a9eed68 100644
--- a/app/services/concerns/contracted.rb
+++ b/app/services/concerns/contracted.rb
@@ -43,10 +43,30 @@ module Concerns::Contracted
@contract_class = cls
end
+ 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 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))
end
def validate_and_save(object, user, options: {})
@@ -77,5 +97,13 @@ module Concerns::Contracted
true
end
end
+
+ 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 }
+ end
end
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 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::SetAttributes
set_attachments_attributes(attributes)
set_static_attributes(attributes)
- work_package.change_by_system do
+ change_by_system do
set_default_attributes(attributes)
update_project_dependent_attributes
end
set_custom_attributes(attributes)
- work_package.change_by_system do
+ change_by_system do
update_dates
reassign_invalid_status_if_type_changed
set_templated_description
@@ -130,7 +130,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def update_project_dependent_attributes
return unless work_package.project_id_changed? && work_package.project_id
- work_package.change_by_system do
+ change_by_system do
set_fixed_version_to_nil
reassign_category
diff --git a/spec/contracts/work_packages/create_contract_spec.rb b/spec/contracts/work_packages/create_contract_spec.rb
index 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 do
work_package.project = project
end
- 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' do
work_package.author = user
- end
- expect(validated_contract.errors[:author_id]).to be_empty
+ expect(validated_contract.errors[:author_id]).to be_empty
+ end
end
- 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]
+ end
end
end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 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 do
it 'notes the author to be system changed' do
subject
- expect(work_package.changed_by_system)
+ expect(instance.changed_by_system)
.to include('author_id')
end
end
@@ -529,7 +529,7 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(work_package.changed_by_system)
+ expect(instance.changed_by_system)
.to include('category_id')
end
end
@@ -558,7 +558,7 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(work_package.changed_by_system)
+ expect(instance.changed_by_system)
.to include('type_id')
end
end
@@ -576,7 +576,7 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(work_package.changed_by_system)
+ expect(instance.changed_by_system)
.to include('type_id')
end
end
@@ -594,7 +594,7 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'does not set the change to system changes' do
subject
- expect(work_package.changed_by_system)
+ expect(instance.changed_by_system)
.not_to include('type_id')
end
end