From 3e87d10980f9ac57b079a8e19bd1e5a8c11c6ece Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Thu, 2 Jul 2015 12:04:47 +0200 Subject: [PATCH] fix bogus errors in contracts - AR tried to read properties that are undefined on the contract (:error_read_only) - now that we actually use Contract properties the copy-validations stuff started to do things, however: - we already went another path to simultaneously validate contract + model - validation was apparently broken, e.g. though the subject was not empty, there were subject :blank errors --- lib/api/contracts/model_contract.rb | 11 +++++++++++ lib/api/v3/work_packages/base_contract.rb | 3 --- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/api/contracts/model_contract.rb b/lib/api/contracts/model_contract.rb index 56618afe2d..d950af84f5 100644 --- a/lib/api/contracts/model_contract.rb +++ b/lib/api/contracts/model_contract.rb @@ -52,6 +52,17 @@ module API end end + # we want to add a validation error whenever someone sets a property that we don't know. + # However AR will cleverly try to resolve the value for errorneous properties. Thus we need + # to hook into this method and return nil for unknown properties to avoid NoMethod errors... + def read_attribute_for_validation(attribute) + if respond_to? attribute + send attribute + end + + nil + end + def writable_attributes collect_ancestor_attributes(:writable_attributes) end diff --git a/lib/api/v3/work_packages/base_contract.rb b/lib/api/v3/work_packages/base_contract.rb index 2d125eeaa7..7c2856da05 100644 --- a/lib/api/v3/work_packages/base_contract.rb +++ b/lib/api/v3/work_packages/base_contract.rb @@ -97,9 +97,6 @@ module API @can = WorkPackagePolicy.new(user) end - extend Reform::Form::ActiveModel::ModelValidations - copy_validations_from WorkPackage - private def validate_people_visible(attribute, id_attribute, list)