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

fixing project API

Jonas Jabari8610e769f0e4
diff --git a/app/contracts/base_contract.rb b/app/contracts/base_contract.rb
index c57c66d1a60..9402f313181 100644
--- a/app/contracts/base_contract.rb
+++ b/app/contracts/base_contract.rb
@@ -222,12 +222,27 @@ class BaseContract < Disposable::Twin
end
if model.respond_to?(:available_custom_fields)
- writable += model.available_custom_fields.map(&:attribute_name)
+ writable += collect_available_custom_field_attributes
end
writable
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.available_custom_fields(global: true).map(&:attribute_name)
+ else
+ model.available_custom_fields.map(&:attribute_name)
+ end
+ end
+
def reduce_writable_attributes(attributes)
attributes = reduce_by_writable_conditions(attributes)
reduce_by_writable_permissions(attributes)
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index e569001ec92..eeee77edd8b 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -35,6 +35,8 @@ class CustomValue < ApplicationRecord
validate :validate_type_of_value
validate :validate_length_of_value
+ after_create :activate_custom_field_in_customized_project, if: -> { customized.is_a?(Project) && value.present? }
+
delegate :typed_value,
:formatted_value,
to: :strategy
@@ -66,6 +68,14 @@ class CustomValue < ApplicationRecord
|| value_is_same_as_default?
end
+ def activate_custom_field_in_customized_project
+ # if a custom value is created for a project via CustomValue.create(...),
+ # the custom field needs to be activated in the project
+ unless customized&.project_custom_fields&.include?(custom_field)
+ customized.project_custom_fields << custom_field
+ end
+ end
+
protected
def value_is_included_in_multi_value_default?
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index ccec9b0548b..0807d96779e 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -40,14 +40,14 @@ module Projects::ActsAsCustomizablePatches
has_many :project_custom_fields, through: :project_custom_field_project_mappings, class_name: 'ProjectCustomField'
before_save :build_missing_project_custom_field_project_mappings
- after_save :reset_section_scoped_validation, :reset_query_available_custom_fields_on_global_level
+ after_save :reset_section_scoped_validation, :set_query_available_custom_fields_to_project_level
# we need to reset the query_available_custom_fields_on_global_level already after validation
# as the update service just calls .valid? and returns if invalid
# after_save is not touched in this case which causes the flag to stay active
- after_validation :reset_query_available_custom_fields_on_global_level
+ after_validation :set_query_available_custom_fields_to_project_level
- before_update :query_available_custom_fields_on_global_level
+ before_update :set_query_available_custom_fields_to_global_level
before_create :reject_section_scoped_validation_for_creation
after_create :disable_custom_fields_with_empty_values
@@ -59,7 +59,7 @@ module Projects::ActsAsCustomizablePatches
custom_field_ids = project.custom_values.reject { |cv| cv.value.blank? }.pluck(:custom_field_id).uniq
activated_custom_field_ids = project_custom_field_project_mappings.pluck(:custom_field_id).uniq
- mappings = (custom_field_ids - activated_custom_field_ids)
+ mappings = (custom_field_ids - activated_custom_field_ids).uniq
.map { |pcf_id| { project_id: id, custom_field_id: pcf_id } }
project_custom_field_project_mappings.build(mappings)
@@ -71,13 +71,13 @@ module Projects::ActsAsCustomizablePatches
self._limit_custom_fields_validation_to_section_id = nil
end
- def query_available_custom_fields_on_global_level
+ def set_query_available_custom_fields_to_global_level
# query the available custom fields on a global level when updating custom field values
# in order to support implicit activation of custom fields when values are provided during an update
self._query_available_custom_fields_on_global_level = true
end
- def reset_query_available_custom_fields_on_global_level
+ def set_query_available_custom_fields_to_project_level
# reset the query_available_custom_fields_on_global_level after saving
# in order not to silently carry this setting in this instance
self._query_available_custom_fields_on_global_level = nil
@@ -103,6 +103,16 @@ module Projects::ActsAsCustomizablePatches
project_custom_field_project_mappings.where(custom_field_id: custom_field_ids).destroy_all
end
+ def query_available_custom_fields_on_global_level
+ # query the available custom fields on a global level when updating custom field values
+ # in order to support implicit activation of custom fields when values are provided during an update
+ self._query_available_custom_fields_on_global_level = true
+ result = yield
+ self._query_available_custom_fields_on_global_level = false
+
+ result
+ end
+
def active_custom_field_ids_of_project
# show all project custom fields in the project creation form
# later on, only those with values will be activated via before_save hook `build_missing_project_custom_field_project_mappings`
@@ -119,7 +129,7 @@ module Projects::ActsAsCustomizablePatches
end
end
- def available_custom_fields
+ def available_custom_fields(global: false)
# overrides acts_as_customizable
# in contrast to acts_as_customizable, custom_fields are enabled per project
# thus we need to check the project_custom_field_project_mappings
@@ -130,7 +140,10 @@ module Projects::ActsAsCustomizablePatches
# we don't want to adjust these calls, but need a way to query the available custom fields on a global level in some cases
# thus we pass in this parameter as an instance flag implicitly here,
# which is not nice but helps us to touch acts_as_customizable as little as possible
- unless _query_available_custom_fields_on_global_level
+ #
+ # additionally we provide the `global` parameter to allow querying the available custom fields on a global level
+ # when we have explicit control over the call of `available_custom_fields`
+ unless global || _query_available_custom_fields_on_global_level
custom_fields = custom_fields.where(id: active_custom_field_ids_of_project)
end
@@ -215,5 +228,18 @@ module Projects::ActsAsCustomizablePatches
end
end
end
+
+ # overrides acts_as_customizable
+ # we need to query the available custom fields on a global level when
+ # trying to set a custom field which is not enabled via e.g. custom_field_123="foo"
+ def for_custom_field_accessor(method_symbol)
+ match = /\Acustom_field_(?<id>\d+)=?\z/.match(method_symbol.to_s)
+ if match
+ custom_field = available_custom_fields(global: true).find { |cf| cf.id.to_s == match[:id] }
+ if custom_field
+ yield custom_field
+ end
+ end
+ end
end
end
diff --git a/spec/models/custom_value_spec.rb b/spec/models/custom_value_spec.rb
index 873e9139b87..e9476f488ea 100644
--- a/spec/models/custom_value_spec.rb
+++ b/spec/models/custom_value_spec.rb
@@ -729,4 +729,16 @@ RSpec.describe CustomValue do
expect(custom_value.value).to eql parsed_value
end
end
+
+ describe '#activate_custom_field_in_customized_project' do
+ let(:custom_field) { create(:project_custom_field) }
+ let(:project) { create(:project) }
+ let(:custom_value) { build(:custom_value, custom_field:, customized: project) }
+
+ it 'activates the custom field in the project after create if missing' do
+ expect(project.project_custom_fields).not_to include(custom_field)
+ custom_value.save!
+ expect(project.reload.project_custom_fields).to include(custom_field)
+ end
+ end
end
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index 6ddbdde236c..153a5f3df59 100644
--- a/spec/models/projects/customizable_spec.rb
+++ b/spec/models/projects/customizable_spec.rb
@@ -296,4 +296,40 @@ RSpec.describe Project, 'customizable' do
it_behaves_like 'implicitly enabled and saved custom values'
end
end
+
+ context 'when updating with custom field setter methods (API approach)' do
+ let(:project) { create(:project) }
+
+ shared_examples 'implicitly enabled and saved custom values' do
+ it 'enables fields with provided values' do
+ # list_custom_field is not provided, thus it should not be enabled
+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)
+ expect(project.project_custom_fields)
+ .to contain_exactly(text_custom_field, bool_custom_field)
+ end
+
+ it 'saves the custom field values properly' do
+ expect(project.custom_value_for(text_custom_field).typed_value)
+ .to eq('foo')
+ expect(project.custom_value_for(bool_custom_field).typed_value)
+ .to be_truthy
+
+ # or via getter methods:
+
+ expect(project.send(:"custom_field_#{text_custom_field.id}")).to eq('foo')
+ expect(project.send(:"custom_field_#{bool_custom_field.id}")).to be_truthy
+ end
+ end
+
+ context 'when setting a value for a disabled custom field' do
+ before do
+ project.send(:"custom_field_#{text_custom_field.id}=", 'foo')
+ project.send(:"custom_field_#{bool_custom_field.id}=", true)
+ project.save!
+ end
+
+ it_behaves_like 'implicitly enabled and saved custom values'
+ end
+ end
end
diff --git a/spec/requests/api/v3/projects/create_resource_spec.rb b/spec/requests/api/v3/projects/create_resource_spec.rb
index 67df535c2ec..3be223c3fc5 100644
--- a/spec/requests/api/v3/projects/create_resource_spec.rb
+++ b/spec/requests/api/v3/projects/create_resource_spec.rb
@@ -129,6 +129,11 @@ RSpec.describe 'API v3 Project resource create', content_type: :json do
.to be_json_eql("CF text".to_json)
.at_path("customField#{custom_field.id}/raw")
end
+
+ it 'automatically activates the cf for project if the value was provided' do
+ expect(Project.last.project_custom_fields)
+ .to contain_exactly(custom_field)
+ end
end
context 'without permission to create projects' do
diff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rb
index 23df8bc0640..e4cf940bc81 100644
--- a/spec/requests/api/v3/projects/update_resource_spec.rb
+++ b/spec/requests/api/v3/projects/update_resource_spec.rb
@@ -106,6 +106,11 @@ RSpec.describe 'API v3 Project resource update', content_type: :json do
expect(project.reload.send(custom_field.attribute_getter))
.to eql("CF text")
end
+
+ it 'automatically activates the cf for project if the value was provided' do
+ expect(project.project_custom_fields)
+ .to contain_exactly(custom_field)
+ end
end
context 'without permission to patch projects' do