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

share model`s error object in contract

ulferts7c30a625b0d2
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index 67dd50a6ab5..42241652d09 100644
--- a/app/contracts/base_contract.rb
+++ b/app/contracts/base_contract.rb
@@ -158,6 +158,14 @@ class BaseContract < Disposable::Twin
ActiveModel::Name.new(model, nil)
end
+ def errors
+ if model.respond_to?(:errors)
+ model.errors
+ else
+ super
+ end
+ end
+
def self.model
@model ||= begin
name.deconstantize.singularize.constantize
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index 84cc1c8c08f..a73616f2b9f 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -32,25 +32,45 @@ require_relative './base_contract'
# Model contract for AR records that
# support change tracking
class ModelContract < BaseContract
- def valid?(*_args)
- super()
- readonly_attributes_unchanged
+ # Runs all the specified validations and returns +true+ if no errors were
+ # added otherwise +false+.
+ # Validations on the model as well as on the contract are run.
+ # Since the error object of this contract is the model's error object,
+ # the errors of both contract and model are both added to it.
+ # After validation, the errors can thus be accessed via both means:
+ #
+ # model = SomeModel.new
+ # contract = SomeModels::SomeModelContract.new(model, some_user)
+ # contract.valid? # => false
+ #
+ # contract.errors == model.errors # => true
+ #
+ # This of course is only true if that contract validates the model and
+ # if the model has an errors object.
+ def valid?(context = nil)
+ model.valid? if validate_model?
- # Allow subclasses to check only contract errors
- return errors.empty? unless validate_model?
+ contract_valid?(context, clear_errors: !validate_model?)
+ end
- model.valid?
+ protected
- # We need to merge the contract errors with the model errors in
- # order to have them available at one place.
- # This is something we need as long as we have validations split
- # among the model and its contract.
- errors.merge!(model.errors)
+ # This method is mostly copied from ActiveModel::Validations#valid?
+ # but:
+ # * does not clear errors before validation unless explicitly instructed to do so.
+ # Clearing would then be done in the #valid? method by calling model.valid?
+ # * Checks for readonly attributes being changed
+ def contract_valid?(context = nil, clear_errors: false)
+ current_context, self.validation_context = validation_context, context # rubocop:disable Style/ParallelAssignment
- errors.empty?
- end
+ errors.clear if clear_errors
- protected
+ readonly_attributes_unchanged
+
+ run_validations!
+ ensure
+ self.validation_context = current_context
+ end
##
# Allow subclasses to disable model validation