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

extract method

ulferts9ed32a276c8e
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index 39f2b7f0ee6..a0540c187f7 100644
--- a/app/contracts/base_contract.rb
+++ b/app/contracts/base_contract.rb
@@ -258,4 +258,12 @@ class BaseContract < Disposable::Twin
true
end
end
+
+ def with_merged_former_errors
+ former_errors = errors.dup
+
+ yield
+
+ errors.merge!(former_errors)
+ end
end
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index 4a6b4550115..8a1c84cbf65 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -123,10 +123,9 @@ module Projects
contract_klass = model.being_archived? ? ArchiveContract : UnarchiveContract
contract = contract_klass.new(model, user)
- former_errors = errors.dup
- contract.validate
-
- errors.merge!(former_errors)
+ with_merged_former_errors do
+ contract.validate
+ end
end
end
end
diff --git a/app/contracts/views/base_contract.rb b/app/contracts/views/base_contract.rb
index a6a2e0204f9..b4a9e07ff8e 100644
--- a/app/contracts/views/base_contract.rb
+++ b/app/contracts/views/base_contract.rb
@@ -42,10 +42,9 @@ module Views
if (strategy_class = Constants::Views.contract_strategy(model.type))
strategy = strategy_class.new(model, user)
- former_errors = errors.dup
- strategy.valid?
-
- errors.merge!(former_errors)
+ with_merged_former_errors do
+ strategy.valid?
+ end
end
errors.empty?
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index b991ba9c26a..2209cdc0c16 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -62,10 +62,9 @@ module Storages::Storages
# Validating the contract will clear the errors
# of this contract so we save them for later.
- former_errors = errors.dup
-
- contract.validate
- errors.merge!(former_errors)
+ with_merged_former_errors do
+ contract.validate
+ end
end
end
end