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

Added support for multi-value Version custom fields (#12948)

Richard "Virgo" Richter7ab5e311ec82
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index ad5571159c9..960f41fa362 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -275,7 +275,7 @@ class CustomField < ApplicationRecord
end
def multi_value_possible?
- %w[user list].include?(field_format) &&
+ %w[version user list].include?(field_format) &&
[ProjectCustomField, WorkPackageCustomField].include?(self.class)
end
diff --git a/frontend/src/app/shared/components/fields/display/display-field.initializer.ts b/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
index 1d3a5481e9b..ef50b104448 100644
--- a/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
+++ b/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
@@ -72,6 +72,7 @@ export function initializeCoreDisplayFields(displayFieldService:DisplayFieldServ
'Category',
'CustomOption'])
.addFieldType(ResourcesDisplayField, 'resources', ['[]CustomOption'])
+ .addFieldType(ResourcesDisplayField, 'resources', ['[]Version'])
.addFieldType(MultipleUserFieldModule, 'users', ['[]User'])
.addFieldType(FormattableDisplayField, 'formattable', ['Formattable'])
.addFieldType(DaysDurationDisplayField, 'duration', ['duration'])
diff --git a/frontend/src/app/shared/components/fields/display/display-field.service.ts b/frontend/src/app/shared/components/fields/display/display-field.service.ts
index 35f4edddb3a..d0487ac6286 100644
--- a/frontend/src/app/shared/components/fields/display/display-field.service.ts
+++ b/frontend/src/app/shared/components/fields/display/display-field.service.ts
@@ -80,6 +80,11 @@ export class DisplayFieldService extends AbstractFieldService<DisplayField, IDis
if (context.container === 'single-view' && isCustomMultiLinesField) {
return new MultipleLinesCustomOptionsDisplayField(fieldName, context) as DisplayField;
}
+ // Separate class seems not needed (merge with []CustomOption above?)
+ const isVersionMultiLinesField = ['[]Version'].indexOf(schema.type) >= 0;
+ if (context.container === 'single-view' && isVersionMultiLinesField) {
+ return new MultipleLinesCustomOptionsDisplayField(fieldName, context) as DisplayField;
+ }
const isUserMultiLinesField = ['[]User'].indexOf(schema.type) >= 0;
if (context.container === 'single-view' && isUserMultiLinesField) {
return new MultipleLinesUserFieldModule(fieldName, context) as DisplayField;
diff --git a/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts b/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
index d4fadf834d1..31e1aa84461 100644
--- a/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
+++ b/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
@@ -70,6 +70,7 @@ export function initializeCoreEditFields(editFieldService:EditFieldService, sele
.addFieldType(MultiSelectEditFieldComponent, 'multi-select', [
'[]CustomOption',
'[]User',
+ '[]Version',
])
.addFieldType(FloatEditFieldComponent, 'float', ['Float'])
.addFieldType(WorkPackageEditFieldComponent, 'workPackage', ['WorkPackage'])
diff --git a/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts b/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
index fbb4d74b139..22c0e1208f7 100644
--- a/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
@@ -311,6 +311,7 @@ export default class CustomFieldsController extends Controller {
this.unsearchable();
break;
case 'version':
+ this.show(...this.multiSelectTargets);
this.activate(this.defaultValueTargets, false);
this.activate(this.possibleValuesTargets, false);
this.hide(...this.lengthTargets, ...this.regexpTargets, ...this.defaultValueTargets);
diff --git a/lib/open_project/journal_formatter/custom_field.rb b/lib/open_project/journal_formatter/custom_field.rb
index 2f85550dc37..f54c098b52e 100644
--- a/lib/open_project/journal_formatter/custom_field.rb
+++ b/lib/open_project/journal_formatter/custom_field.rb
@@ -57,6 +57,8 @@ class OpenProject::JournalFormatter::CustomField < JournalFormatter::Base
:find_list_value
when 'user'
:find_user_value
+ when 'version'
+ :find_version_value
else
:format_value
end
@@ -82,13 +84,7 @@ class OpenProject::JournalFormatter::CustomField < JournalFormatter::Base
.where(id: ids)
.index_by(&:id)
- ids.map do |id|
- if user_lookup.key?(id)
- user_lookup[id].name
- else
- I18n.t(:label_missing_or_hidden_custom_option)
- end
- end.join(', ')
+ ids_to_names(ids, user_lookup)
end
def find_list_value(value, custom_field)
@@ -104,4 +100,27 @@ class OpenProject::JournalFormatter::CustomField < JournalFormatter::Base
id_value[id] || I18n.t(:label_deleted_custom_option)
end.join(', ')
end
+
+ def find_version_value(value, _custom_field)
+ ids = value.split(",").map(&:to_i)
+
+ # Lookup visible versions we can find
+ version_lookup =
+ Version
+ .visible(User.current)
+ .where(id: ids)
+ .index_by(&:id)
+
+ ids_to_names(ids, version_lookup)
+ end
+
+ def ids_to_names(ids, id_to_name_lookup)
+ ids.map do |id|
+ if id_to_name_lookup.key?(id)
+ id_to_name_lookup[id].name
+ else
+ I18n.t(:label_missing_or_hidden_custom_option)
+ end
+ end.join(', ')
+ end
end
diff --git a/spec/features/activities/project_attributes_activity_spec.rb b/spec/features/activities/project_attributes_activity_spec.rb
index 68507def74e..26e9fd9abad 100644
--- a/spec/features/activities/project_attributes_activity_spec.rb
+++ b/spec/features/activities/project_attributes_activity_spec.rb
@@ -29,16 +29,9 @@
require 'spec_helper'
RSpec.describe 'Project attributes activity', :js, :with_cuprite do
- let(:user) do
- create(:user,
- member_in_project: project,
- member_with_permissions: %w[view_wiki_pages
- edit_wiki_pages
- view_wiki_edits])
- end
-
+ let(:user) { create(:user, member_in_project: project) }
let(:parent_project) { create(:project, name: 'parent') }
- let(:project) { create(:project, parent: parent_project, active: false, enabled_module_names: %w[activity]) }
+ let(:project) { create(:project, parent: parent_project, active: false) }
let!(:list_project_custom_field) { create(:list_project_custom_field) }
let!(:version_project_custom_field) { create(:version_project_custom_field) }
@@ -50,6 +43,7 @@ RSpec.describe 'Project attributes activity', :js, :with_cuprite do
let!(:string_project_custom_field) { create(:string_project_custom_field) }
let!(:date_project_custom_field) { create(:date_project_custom_field) }
+ let(:old_version) { create(:version, project:, name: 'Ringbo 1.0') }
let(:next_version) { create(:version, project:, name: 'Turfu 2.0') }
current_user { user }
@@ -66,7 +60,7 @@ RSpec.describe 'Project attributes activity', :js, :with_cuprite do
active: true,
templated: true,
list_project_custom_field.attribute_name => list_project_custom_field.possible_values.first.id,
- version_project_custom_field.attribute_name => next_version.id,
+ version_project_custom_field.attribute_name => [old_version.id, next_version.id],
bool_project_custom_field.attribute_name => true,
user_project_custom_field.attribute_name => current_user.id,
int_project_custom_field.attribute_name => 42,
@@ -108,7 +102,8 @@ RSpec.describe 'Project attributes activity', :js, :with_cuprite do
# custom fields
expect(page).to have_selector('li', text: "#{list_project_custom_field.name} " \
"set to #{project.send(list_project_custom_field.attribute_getter)}")
- expect(page).to have_selector('li', text: "#{version_project_custom_field.name} set to #{next_version.name}")
+ expect(page).to have_selector('li', text: "#{version_project_custom_field.name} " \
+ "set to #{old_version.name}, #{next_version.name}")
expect(page).to have_selector('li', text: "#{bool_project_custom_field.name} set to Yes")
expect(page).to have_selector('li', text: "#{user_project_custom_field.name} set to #{current_user.name}")
expect(page).to have_selector('li', text: "#{int_project_custom_field.name} set to 42")
diff --git a/spec/features/custom_fields/multi_version_custom_field_spec.rb b/spec/features/custom_fields/multi_version_custom_field_spec.rb
new file mode 100644
index 00000000000..a7cbcba004a
--- /dev/null
+++ b/spec/features/custom_fields/multi_version_custom_field_spec.rb
@@ -0,0 +1,116 @@
+require "spec_helper"
+require "support/pages/work_packages/abstract_work_package"
+
+RSpec.describe "multi version custom field", js: true do
+ shared_let(:admin) { create(:admin) }
+ let(:current_user) { admin }
+ let(:wp_page) { Pages::FullWorkPackage.new work_package }
+ let(:cf_edit_field) do
+ field = wp_page.edit_field custom_field.attribute_name(:camel_case)
+ field.field_type = 'create-autocompleter'
+ field
+ end
+ let(:work_package) { create(:work_package, project:, type:) }
+ let!(:version_old) { create(:version, project:, name: 'Version Old') }
+ let!(:version_current) { create(:version, project:, name: 'Version Current') }
+ let!(:version_future) { create(:version, project:, name: 'Version Future') }
+
+ shared_let(:type) { create(:type) }
+ shared_let(:project) { create(:project, types: [type]) }
+ shared_let(:role) { create(:role) }
+
+ shared_let(:custom_field) do
+ create(
+ :version_wp_custom_field,
+ name: "Fix version",
+ multi_value: true,
+ types: [type],
+ projects: [project]
+ )
+ end
+
+ before do
+ login_as current_user
+ wp_page.visit!
+ wp_page.ensure_page_loaded
+ end
+
+ it "is shown and allowed to be updated" do
+ expect(page).to have_text custom_field.name
+
+ cf_edit_field.activate!
+ cf_edit_field.set_value "Version Old"
+ cf_edit_field.set_value "Version Current"
+ cf_edit_field.set_value "Version Future"
+
+ cf_edit_field.submit_by_dashboard
+
+ expect(page).to have_text custom_field.name
+ expect(page).to have_text "Version Old"
+ expect(page).to have_text "Version Current"
+ expect(page).to have_text "Version Future"
+
+ wp_page.expect_and_dismiss_toaster(message: "Successful update.")
+
+ work_package.reload
+ cvs = work_package
+ .custom_value_for(custom_field)
+ .map(&:typed_value)
+
+ expect(cvs).to contain_exactly(version_old, version_current, version_future)
+
+ cf_edit_field.activate!
+ cf_edit_field.unset_value "Version Current", multi: true
+ cf_edit_field.unset_value "Version Future", multi: true
+ cf_edit_field.submit_by_dashboard
+
+ wp_page.expect_and_dismiss_toaster(message: "Successful update.")
+
+ expect(page).to have_text "Version Old"
+ expect(page).not_to have_text "Version Current"
+ expect(page).not_to have_text "Version Future"
+
+ work_package.reload
+
+ # only one value, so no array
+ cvs = work_package
+ .custom_value_for(custom_field)
+ .typed_value
+
+ expect(cvs).to eq version_old
+ end
+
+ context "with existing version values" do
+ let(:work_package) do
+ wp = build(:work_package, project:, type:)
+
+ wp.custom_field_values = {
+ custom_field.id => [version_old.id.to_s, version_current.id.to_s]
+ }
+
+ wp.save
+ wp
+ end
+
+ it "is shown and allowed to be updated" do
+ expect(page).to have_text custom_field.name
+ expect(page).to have_text "Version Old"
+ expect(page).to have_text "Version Current"
+
+ page.find(".inline-edit--display-field", text: "Version Old").click
+
+ cf_edit_field.unset_value "Version Old", multi: true
+ cf_edit_field.set_value "Version Future"
+
+ click_on "Fix version: Save"
+ wp_page.expect_and_dismiss_toaster(message: "Successful update.")
+ # .customField<ID> above is required to ignore Assignee and Accountable which are not interesting for us.
+ expect(page).to have_selector(".customField#{custom_field.id} .custom-option", count: 2)
+
+ expect(page).to have_text custom_field.name
+ expect(page).to have_text "Version Current"
+ expect(page).to have_text "Version Future"
+ expect(page).not_to have_text "Version Old"
+ end
+ end
+end