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

allow our models to have non existing attributes read for validation

ulferts976fccb296df
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index 42241652d09..13c03d7d095 100644
--- a/app/contracts/base_contract.rb
+++ b/app/contracts/base_contract.rb
@@ -125,6 +125,7 @@ class BaseContract < Disposable::Twin
@options = options
end
+ # TODO: This should be removable here and in the subclasses
# 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 erroneous properties. Thus we need
# to hook into this method and return nil for unknown properties to avoid NoMethod errors...
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index e965d56a42a..74eef7872d4 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -15,6 +15,13 @@ class ApplicationRecord < ActiveRecord::Base
errors[attribute].empty?
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 erroneous 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)
+ super if respond_to?(attribute)
+ end
+
##
# Get the newest recently changed resource for the given record classes
#
diff --git a/lib/api/errors/error_base.rb b/lib/api/errors/error_base.rb
index 3067cfc3828..ef250bc9e4a 100644
--- a/lib/api/errors/error_base.rb
+++ b/lib/api/errors/error_base.rb
@@ -88,7 +88,7 @@ module API
errors.attribute_names.each do |attribute|
api_attribute_name = ::API::Utilities::PropertyNameConverter.from_ar_name(attribute)
- errors.symbols_and_messages_for(attribute).each do |symbol, message|
+ symbols_and_messages_for(errors, attribute).each do |symbol, message|
api_errors << if symbol == :error_readonly
::API::Errors::UnwritableProperty.new(api_attribute_name, message)
else
@@ -99,6 +99,13 @@ module API
api_errors
end
+
+ def symbols_and_messages_for(errors, attribute)
+ symbols = errors.details[attribute].pluck(:error)
+ messages = errors.full_messages_for(attribute)
+
+ symbols.zip(messages)
+ end
end
def initialize(message, **)
diff --git a/lib/open_project/patches/active_model_errors.rb b/lib/open_project/patches/active_model_errors.rb
index db75c512e8f..5cef22860df 100644
--- a/lib/open_project/patches/active_model_errors.rb
+++ b/lib/open_project/patches/active_model_errors.rb
@@ -27,16 +27,8 @@
#++
# This patch should no longer be necessary.
-# But we have references to symbolds_and_messages_for as well as for symbols_for all over
-# the code base.
+# But we have references to symbols_for still in the code base.
module OpenProject::ActiveModelErrorsPatch
- def symbols_and_messages_for(attribute)
- symbols = details[attribute].pluck(:error)
- messages = full_messages_for(attribute)
-
- symbols.zip(messages)
- end
-
def symbols_for(attribute)
details[attribute].pluck(:error)
end