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

Move Project's available custom fields overrides to their corresponding Project contract class

Dombi Attila2ed8c330a29d
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index e92d1326aae..c420b75f00e 100644
--- a/app/contracts/base_contract.rb
+++ b/app/contracts/base_contract.rb
@@ -226,18 +226,7 @@ class BaseContract < Disposable::Twin
end
def collect_available_custom_field_attributes
- if model.is_a?(Project)
- # required because project custom fields are now activated on a per-project basis
- #
- # if we wouldn't query available_custom field on a global level here,
- # implicitly enabling project custom fields through this contract would fail
- # as the disabled custom fields would be treated as not-writable
- #
- # relevant especially for the project API
- model.all_available_custom_fields.visible.map(&:attribute_name)
- else
- model.available_custom_fields.map(&:attribute_name)
- end
+ model.available_custom_fields.map(&:attribute_name)
end
def reduce_writable_attributes(attributes)
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index a894a0e2ffe..3c9584e3eb5 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -77,6 +77,20 @@ module Projects
Project.status_codes.keys
end
+ protected
+
+ def collect_available_custom_field_attributes
+ # required because project custom fields are now activated on a per-project basis
+ #
+ # if we wouldn't query available_custom field on a global level here,
+ # implicitly enabling project custom fields through this contract would fail
+ # as the disabled custom fields would be treated as not-writable
+ #
+ # relevant especially for the project API
+
+ model.all_available_custom_fields.map(&:attribute_name)
+ end
+
private
def validate_parent_assignable
diff --git a/app/contracts/projects/create_contract.rb b/app/contracts/projects/create_contract.rb
index 78b38a775c7..bc922099e22 100644
--- a/app/contracts/projects/create_contract.rb
+++ b/app/contracts/projects/create_contract.rb
@@ -33,6 +33,12 @@ module Projects
# so allowing writing here would be useless.
allow_writable_timestamps :created_at
+ protected
+
+ def collect_available_custom_field_attributes
+ model.all_visible_custom_fields.map(&:attribute_name)
+ end
+
private
def validate_user_allowed_to_manage
diff --git a/app/models/projects/custom_fields.rb b/app/models/projects/custom_fields.rb
index 7c042adb540..92be68bbd5c 100644
--- a/app/models/projects/custom_fields.rb
+++ b/app/models/projects/custom_fields.rb
@@ -39,11 +39,10 @@ module Projects::CustomFields
class_name: "ProjectCustomField"
def available_custom_fields
- visible_fields = all_available_custom_fields.visible
- return visible_fields if new_record?
+ return all_visible_custom_fields if new_record?
- visible_fields.where(id: project_custom_field_project_mappings.select(:custom_field_id))
- .or(ProjectCustomField.required)
+ all_visible_custom_fields.where(id: project_custom_field_project_mappings.select(:custom_field_id))
+ .or(ProjectCustomField.required)
end
# Note:
@@ -61,6 +60,10 @@ module Projects::CustomFields
.order("custom_field_sections.position", :position_in_custom_field_section)
end
+ def all_visible_custom_fields
+ all_available_custom_fields.visible
+ end
+
def custom_field_values_to_validate
# Limit the set of available custom fields when the validation is limited to a section
if _limit_custom_fields_validation_to_section_id