hide error merging behind contract.validate

pull/3166/head
Jan Sandbrink 10 years ago
parent e22334a5f6
commit 88d7944bb1
  1. 12
      lib/api/contracts/model_contract.rb
  2. 5
      lib/api/v3/work_packages/work_packages_api.rb

@ -55,25 +55,21 @@ module API
validate :readonly_attributes_unchanged
validate :run_attribute_validations
# Performs validation of both, the contract and the underlying model and returns
# the merged validation errors for both of them.
def validate_contract_and_model
self.validate
def validate
super.validate
model.valid?
merged_errors = self.errors.dup
# 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.
model.errors.keys.each do |key|
model.errors[key].each do |message|
merged_errors.add(key, message)
errors.add(key, message)
end
end
merged_errors
errors.empty?
end
private

@ -70,13 +70,12 @@ module API
send_notifications: send_notifications)
contract = UpdateContract.new(@work_package, current_user)
errors = contract.validate_contract_and_model
if errors.empty? && update_service.save
if contract.validate && update_service.save
@work_package.reload
work_package_representer
else
fail ::API::Errors::ErrorBase.create(errors)
fail ::API::Errors::ErrorBase.create(contract.errors)
end
end

Loading…
Cancel
Save