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
Replace with_merged_former_errors by validate_and_merge_errors
Jan Sandbrinkc753ce6e3094
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rbindex 6e91a78285c..cc9b288889c 100644--- a/app/contracts/base_contract.rb+++ b/app/contracts/base_contract.rb@@ -283,11 +283,29 @@ class BaseContract < Disposable::Twinendend- def with_merged_former_errors+ def validate_and_merge_errors(contract)+ # Duplicating errors beforehand, in case the other contract shares+ # the errors with us (because by default contracts reuse the errors+ # of the model they validate) to prevent that existing errors are cleaned+ # during validationformer_errors = errors.dup- yield+ contract.validate- errors.merge!(former_errors)+ # Another side effect of sharing errors is that they may have already been added to+ # our list of errors, after the validation was executed, so we manually filter out+ # duplicates.+ # To avoid looping over all the errors we just added (forever), we copy the errors of the+ # contract into an array first+ contract_errors = contract.errors.each.to_a+ merge_errors_deduplicated(former_errors.chain(contract_errors))+ end++ def merge_errors_deduplicated(other_errors)+ other_errors.each do |e|+ # This is why we can't have nice things: https://github.com/rails/rails/issues/54483+ options = e.options.except(*ActiveModel::Error::CALLBACKS_OPTIONS + ActiveModel::Error::MESSAGE_OPTIONS)+ errors.add(e.attribute, e.type, **e.options) unless errors.added?(e.attribute, e.type, options)+ endendenddiff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex 614b6acb248..3188d4f09f1 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -144,9 +144,7 @@ module Projectscontract_klass = model.being_archived? ? ArchiveContract : UnarchiveContractcontract = contract_klass.new(model, user)- with_merged_former_errors do- contract.validate- end+ validate_and_merge_errors(contract)endendenddiff --git a/app/contracts/views/base_contract.rb b/app/contracts/views/base_contract.rbindex 008a53b1ff6..12e506fd3b1 100644--- a/app/contracts/views/base_contract.rb+++ b/app/contracts/views/base_contract.rb@@ -42,9 +42,7 @@ module Viewsif (strategy_class = Constants::Views.contract_strategy(model.type))strategy = strategy_class.new(model, user)- with_merged_former_errors do- strategy.valid?- end+ validate_and_merge_errors(strategy)enderrors.empty?diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rbindex 9ba945989d2..ea53150832f 100644--- a/modules/storages/app/contracts/storages/storages/base_contract.rb+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb@@ -61,11 +61,7 @@ module Storages::Storages# Otherwise, we get :readonly validation errors.contract.writable_attributes.append(*writable_attributes)- # Validating the contract will clear the errors- # of this contract so we save them for later.- with_merged_former_errors do- contract.validate- end+ validate_and_merge_errors(contract)enddef require_ee_token_for_one_drivediff --git a/spec/contracts/base_contract_spec.rb b/spec/contracts/base_contract_spec.rbindex 8eb4dfdd468..7ee17df8eda 100644--- a/spec/contracts/base_contract_spec.rb+++ b/spec/contracts/base_contract_spec.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -29,7 +31,8 @@require "spec_helper"RSpec.describe BaseContract do- let(:model) { double(name: "name") } # rubocop:disable RSpec/VerifiedDoubles+ let(:model) { WorkPackage.new }+ let(:current_errors) { model.errors }let(:user) { build(:user) }describe ".writable_attributes" do@@ -255,4 +258,36 @@ RSpec.describe BaseContract doit_behaves_like "the parent writable parameter is overridden by the child writable parameter"endend++ describe "#validate_and_merge_errors" do+ subject { current_contract.send(:validate_and_merge_errors, other_contract) }++ let(:current_contract) { described_class.new(model, user) }+ let(:other_contract) { double("contract", errors: other_errors) } # rubocop:disable RSpec/VerifiedDoubles++ before do+ current_errors.add(:base, :invalid)+ allow(other_contract).to receive(:validate) do+ other_contract.errors.add(:base, :blank)+ end+ end++ context "when the other contract has an own #errors instance" do+ let(:other_errors) { ActiveModel::Errors.new(nil) }++ it "merges errors of current contract and other contract" do+ subject+ expect(current_contract.errors.details[:base].map { |d| d[:error] }).to eq(%i[invalid blank])+ end+ end++ context "when the other contract shares errors instance with current contract" do+ let(:other_errors) { current_errors }++ it "merges errors of current contract and other contract (without duplicates)" do+ subject+ expect(current_contract.errors.details[:base].map { |d| d[:error] }).to eq(%i[invalid blank])+ end+ end+ endend