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

Correctly create default values for multi-value list custom field

Christophe Bliard0029bffe3d5c
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 960f41fa362..57554dfa4fb 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -80,7 +80,7 @@ class CustomField < ApplicationRecord
def default_value
if list?
- ids = custom_options.select(&:default_value).map(&:id)
+ ids = custom_options.where(default_value: true).pluck(:id).map(&:to_s)
if multi_value?
ids
@@ -181,24 +181,26 @@ class CustomField < ApplicationRecord
end
def cast_value(value)
- casted = nil
- if value.present?
- case field_format
- when 'string', 'text', 'list'
- casted = value
- when 'date'
- casted = begin; value.to_date; rescue StandardError; nil end
- when 'bool'
- casted = ActiveRecord::Type::Boolean.new.cast(value)
- when 'int'
- casted = value.to_i
- when 'float'
- casted = value.to_f
- when 'user', 'version'
- casted = (value.blank? ? nil : field_format.classify.constantize.find_by(id: value.to_i))
+ return if value.blank?
+
+ case field_format
+ when 'string', 'text', 'list'
+ value
+ when 'date'
+ begin
+ value.to_date
+ rescue StandardError
+ nil
end
+ when 'bool'
+ ActiveRecord::Type::Boolean.new.cast(value)
+ when 'int'
+ value.to_i
+ when 'float'
+ value.to_f
+ when 'user', 'version'
+ field_format.classify.constantize.find_by(id: value.to_i)
end
- casted
end
def <=>(other)
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index f47932f69ee..88f32a1b4e7 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -57,16 +57,28 @@ class CustomValue < ApplicationRecord
def strategy
@strategy ||= begin
format = custom_field&.field_format || 'empty'
- OpenProject::CustomFieldFormat.find_by_name(format).formatter.new(self)
+ OpenProject::CustomFieldFormat.find_by_name(format).formatter.new(self) # rubocop:disable Rails/DynamicFindBy
end
end
def default?
- custom_field.cast_value(value) == custom_field.default_value
+ value_is_included_in_multi_value_default? \
+ || value_is_same_as_default?
end
protected
+ def value_is_included_in_multi_value_default?
+ return false unless custom_field.multi_value?
+
+ [custom_field.default_value, value].all?(&:blank?) \
+ || custom_field.default_value&.include?(custom_field.cast_value(value))
+ end
+
+ def value_is_same_as_default?
+ custom_field.cast_value(value) == custom_field.default_value
+ end
+
def validate_presence_of_required_value
errors.add(:value, :blank) if custom_field.required? && !strategy.value_present?
end
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index ae2108a35d9..cfbca63c893 100644
--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -117,13 +117,10 @@ module Redmine
existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }
if existing_cvs.empty?
- new_value = custom_values.build(customized: self,
- custom_field:,
- value: custom_field.default_value)
- existing_cvs.push new_value
+ build_default_custom_values(custom_field)
+ else
+ existing_cvs
end
-
- existing_cvs
end
end
@@ -283,10 +280,10 @@ module Redmine
end
end
- def method_missing(method, *args)
+ def method_missing(method, *)
for_custom_field_accessor(method) do |custom_field|
add_custom_field_accessors(custom_field)
- return send method, *args
+ return send(method, *)
end
super
@@ -312,6 +309,24 @@ module Redmine
private
+ def build_default_custom_values(custom_field)
+ if custom_field.multi_value? && custom_field.default_value.present?
+ custom_field.default_value.map do |value|
+ build_custom_value(custom_field, value:)
+ end
+ elsif custom_field.multi_value? && custom_field.default_value.blank?
+ build_custom_value(custom_field, value: nil)
+ else
+ build_custom_value(custom_field, value: custom_field.default_value)
+ end
+ end
+
+ def build_custom_value(custom_field, value:)
+ custom_values.build(customized: self,
+ custom_field:,
+ value:)
+ end
+
def for_custom_field_accessor(method_symbol)
match = /\Acustom_field_(?<id>\d+)=?\z/.match(method_symbol.to_s)
if match
diff --git a/spec/factories/custom_field_factory.rb b/spec/factories/custom_field_factory.rb
index c124f9fdd16..deaa85ca4f9 100644
--- a/spec/factories/custom_field_factory.rb
+++ b/spec/factories/custom_field_factory.rb
@@ -44,6 +44,104 @@ FactoryBot.define do
RequestStore.clear!
end
+ trait :boolean do
+ field_format { 'bool' }
+ end
+
+ trait :string do
+ field_format { 'string' }
+ end
+
+ trait :text do
+ field_format { 'text' }
+ end
+
+ trait :int do
+ field_format { 'int' }
+ end
+
+ trait :float do
+ field_format { 'float' }
+ end
+
+ trait :date do
+ field_format { 'date' }
+ end
+
+ trait :list do
+ transient do
+ default_option { nil }
+ default_options { nil }
+ end
+ field_format { 'list' }
+ multi_value { false }
+ possible_values { ['A', 'B', 'C', 'D', 'E', 'F', 'G'] }
+
+ # update custom options default value from the default_option transient
+ # field for non-multiselect field
+ after(:create) do |custom_field, evaluator|
+ default_option = evaluator.default_option
+ next if default_option.blank?
+
+ # ensure the right options are used
+ if evaluator.multi_value
+ raise "Please use default_options instead of default_option for multi_value list field."
+ end
+
+ if default_option.is_a?(Array)
+ raise "default_option #{default_option.inspect} is an array but the list custom field is not a multi_value." \
+ "Please use a single value instead."
+ end
+
+ default_custom_option = custom_field.possible_values.find_by(value: default_option)
+ if default_custom_option.nil?
+ raise "Default option #{default_option.inspect} not found. " \
+ "Possible options are #{custom_field.possible_values.pluck(:value).inspect}"
+ end
+
+ default_custom_option.update!(default_value: true)
+ end
+
+ # update custom options default value from the default_options transient
+ # field for multiselect field
+ after(:create) do |custom_field, evaluator|
+ default_options = Array(evaluator.default_options)
+ next if default_options.blank?
+
+ default_custom_options = custom_field.possible_values.where(value: default_options)
+ if default_custom_options.size != default_options.size
+ not_found_options = default_options - default_custom_options.pluck(:value)
+ raise "Default options #{not_found_options.inspect} not found. " \
+ "Possible options are #{custom_field.possible_values.pluck(:value).inspect}"
+ end
+
+ default_custom_options.update_all(default_value: true)
+ end
+ end
+
+ trait :multi_list do
+ list
+ multi_value { true }
+ end
+
+ trait :version do
+ field_format { 'version' }
+ end
+
+ trait :multi_version do
+ field_format { 'version' }
+ multi_value { true }
+ end
+
+ trait :user do
+ field_format { 'user' }
+ end
+
+ trait :multi_user do
+ field_format { 'user' }
+ multi_value { true }
+ end
+
factory :project_custom_field, class: 'ProjectCustomField' do
sequence(:name) { |n| "Project custom field #{n}" }
diff --git a/spec/features/custom_fields/multi_user_custom_field_spec.rb b/spec/features/custom_fields/multi_user_custom_field_spec.rb
index f7e83df0d2c..8eb8b748873 100644
--- a/spec/features/custom_fields/multi_user_custom_field_spec.rb
+++ b/spec/features/custom_fields/multi_user_custom_field_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe "multi select custom values",
wait_for_reload
end
- describe 'with mixed users, group, and placeholdders' do
+ describe 'with mixed users, group, and placeholders' do
let(:work_package) { create(:work_package, project:, type:) }
let!(:user) do
diff --git a/spec/lib/custom_field_form_builder_spec.rb b/spec/lib/custom_field_form_builder_spec.rb
index 044a634123f..de791c9671a 100644
--- a/spec/lib/custom_field_form_builder_spec.rb
+++ b/spec/lib/custom_field_form_builder_spec.rb
@@ -192,11 +192,11 @@ RSpec.describe CustomFieldFormBuilder do
context 'for a list custom field' do
let(:custom_field) do
- build_stubbed(:list_wp_custom_field,
- custom_options: [custom_option])
+ create(:list_wp_custom_field,
+ custom_options: [custom_option])
end
let(:custom_option) do
- build_stubbed(:custom_option, value: 'my_option')
+ create(:custom_option, value: 'my_option')
end
it_behaves_like 'wrapped in container', 'select-container' do
@@ -216,7 +216,7 @@ RSpec.describe CustomFieldFormBuilder do
context 'which is required and has no default value' do
before do
- custom_field.is_required = true
+ custom_field.update(is_required: true)
end
it 'outputs element' do
@@ -233,8 +233,8 @@ RSpec.describe CustomFieldFormBuilder do
context 'which is required and a default value' do
before do
- custom_field.is_required = true
- custom_option.default_value = true
+ custom_field.update(is_required: true)
+ custom_option.update(default_value: true)
end
it 'outputs element' do
diff --git a/spec/models/custom_value_spec.rb b/spec/models/custom_value_spec.rb
index 3caceb0a48a..7a6a60b3a79 100644
--- a/spec/models/custom_value_spec.rb
+++ b/spec/models/custom_value_spec.rb
@@ -32,7 +32,7 @@ RSpec.describe CustomValue do
shared_let(:version) { create(:version) }
let(:format) { 'bool' }
- let(:custom_field) { create(:custom_field, field_format: format) }
+ let(:custom_field) { create(:version_custom_field, field_format: format) }
let(:custom_value) { create(:custom_value, custom_field:, value:, customized: version) }
describe '#typed_value' do
@@ -98,6 +98,287 @@ RSpec.describe CustomValue do
end
end
+ describe '#default?' do
+ shared_let(:project) { create(:project) }
+
+ RSpec::Matchers.define_negated_matcher :not_be_default, :be_default
+
+ shared_examples 'returns true for generated custom value' do
+ describe "for a generated custom value" do
+ it 'returns true' do
+ custom_values = project.custom_field_values
+
+ expect(custom_values.count).to eq(1)
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ shared_examples 'returns false for custom value with value' do |value:|
+ describe "for a custom value with #{value.inspect} value" do
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, value)
+ custom_value = project.custom_values.last
+
+ expect(custom_value).to not_be_default
+ end
+ end
+ end
+
+ context 'for a boolean custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :boolean) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: false
+ include_examples 'returns false for custom value with value', value: true
+ end
+
+ context 'for a boolean custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :boolean, default_value: true)
+ create(:project_custom_field, :boolean, default_value: false)
+ # the admin interface saves default value as "1" (checked) or "0" (unchecked)
+ create(:project_custom_field, :boolean, default_value: '1')
+ create(:project_custom_field, :boolean, default_value: '0')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context "for a string custom field without default value" do
+ shared_let(:custom_field) { create(:project_custom_field, :string) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: 'Hello world!'
+ end
+
+ context 'for a string custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :string, default_value: 'Hello world!')
+ create(:project_custom_field, :string, default_value: '')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for a text custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :text) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: "Hello world!"
+ include_examples 'returns false for custom value with value', value: "Hello world!\nHello world!\nHello world!"
+ end
+
+ context 'for a text custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :text, default_value: 'Hello world!')
+ create(:project_custom_field, :text, default_value: '')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for an integer custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :int) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: 123
+ include_examples 'returns false for custom value with value', value: 0
+ include_examples 'returns false for custom value with value', value: -12
+ end
+
+ context 'for an integer custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :int, default_value: 0)
+ create(:project_custom_field, :int, default_value: 123)
+ create(:project_custom_field, :int, default_value: '456')
+ create(:project_custom_field, :int, default_value: -987)
+ create(:project_custom_field, :int, default_value: '-678')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for a float custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :float) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: 3.14
+ include_examples 'returns false for custom value with value', value: 0
+ include_examples 'returns false for custom value with value', value: -12
+ end
+
+ context 'for a float custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :float, default_value: 0.0)
+ create(:project_custom_field, :float, default_value: 12.3)
+ create(:project_custom_field, :float, default_value: '45.6')
+ create(:project_custom_field, :float, default_value: -98.7)
+ create(:project_custom_field, :float, default_value: '-67')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for a date custom field' do
+ shared_let(:custom_field) { create(:project_custom_field, :date) }
+
+ include_examples 'returns true for generated custom value'
+ include_examples 'returns false for custom value with value', value: "2023-08-08"
+ include_examples 'returns false for custom value with value', value: Date.current
+ end
+
+ context 'for a list custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :list) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe "for a custom value with option 'B' selected" do
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, custom_field.value_of('B'))
+ custom_value = project.custom_values.last
+
+ expect(custom_value).to not_be_default
+ end
+ end
+ end
+
+ context 'for a list custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :list, default_option: 'B')
+
+ custom_values = project.custom_field_values
+
+ expect(custom_values.count).to eq(ProjectCustomField.count)
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for a multi-value list custom field without default value' do
+ shared_let(:custom_field) { create(:project_custom_field, :multi_list) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe "for a custom value with option 'B' and 'D' selected" do
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, [custom_field.value_of('B'), custom_field.value_of('D')])
+ project.save!
+
+ expect(project.custom_values).to all(not_be_default)
+ end
+ end
+ end
+
+ context 'for a multi-value list custom field with default value' do
+ describe 'for a generated custom value' do
+ it 'returns true' do
+ create(:project_custom_field, :multi_list, default_options: ['B'])
+ create(:project_custom_field, :multi_list, default_options: ['G', 'B', 'C'])
+
+ custom_values = project.custom_field_values
+
+ # 1 CustomValue for each of the default options
+ expect(custom_values.count).to eq(4)
+ expect(custom_values).to all(be_default)
+ end
+ end
+ end
+
+ context 'for a version custom field' do
+ shared_let(:custom_field) { create(:project_custom_field, :version) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe 'for a custom value with a version selected' do
+ let!(:version_turfu) { create(:version, name: 'turfu', project:) }
+
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, version_turfu.id)
+ project.save!
+ custom_value = project.custom_values.last
+
+ expect(custom_value).to not_be_default
+ end
+ end
+ end
+
+ context 'for a multi version custom field' do
+ shared_let(:custom_field) { create(:project_custom_field, :multi_version) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe 'for a custom value with multiple versions selected' do
+ let!(:version_ringbo) { create(:version, name: 'ringbo', project:) }
+ let!(:version_turfu) { create(:version, name: 'turfu', project:) }
+
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, [version_ringbo.id, version_turfu.id])
+ project.save!
+
+ expect(project.custom_values).to all(not_be_default)
+ end
+ end
+ end
+
+ context 'for a user custom field' do
+ shared_let(:custom_field) { create(:project_custom_field, :user) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe 'for a custom value with a user selected' do
+ let!(:alice) { create(:user, firstname: 'Alice', member_in_project: project) }
+
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, alice.id)
+ project.save!
+ custom_value = project.custom_values.last
+
+ expect(custom_value).to not_be_default
+ end
+ end
+ end
+
+ context 'for a multi user custom field' do
+ shared_let(:custom_field) { create(:project_custom_field, :multi_user) }
+
+ include_examples 'returns true for generated custom value'
+
+ describe 'for a custom value with multiple users selected' do
+ let!(:alice) { create(:user, firstname: 'Alice', member_in_project: project) }
+ let!(:bob) { create(:user, firstname: 'Bob', member_in_project: project) }
+
+ it 'returns false' do
+ project.send(custom_field.attribute_setter, [alice.id, bob.id])
+ project.save!
+
+ expect(project.custom_values).to all(not_be_default)
+ end
+ end
+ end
+ end
+
describe 'trying to use a custom field that does not exist' do
subject { build(:custom_value, custom_field_id: 123412341, value: 'my value') }
diff --git a/spec/models/work_package/work_package_acts_as_customizable_spec.rb b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
index c982a69637d..f4e2312f93b 100644
--- a/spec/models/work_package/work_package_acts_as_customizable_spec.rb
+++ b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
@@ -75,6 +75,56 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
end
end
+ describe '#custom_field_values' do
+ subject(:work_package) do
+ setup_custom_field(custom_field)
+ new_work_package
+ end
+
+ context 'with a multi-value list custom field without default value' do
+ let(:custom_field) { create(:wp_custom_field, :multi_list) }
+
+ it 'returns an array with a CustomValue with nil value' do
+ expect(work_package.custom_field_values)
+ .to match([
+ an_instance_of(CustomValue).and(having_attributes(value: nil, custom_field_id: custom_field.id))
+ ])
+ end
+ end
+
+ context 'with a multi-value list custom field with default value of 1 option' do
+ let(:custom_field) { create(:wp_custom_field, :multi_list, default_options: ['B']) }
+
+ it 'returns an array with a CustomValue whose value is the stringified id of the default custom option' do
+ option_b = custom_field.custom_options.find_by(value: 'B')
+ expect(work_package.custom_field_values)
+ .to match([
+ an_instance_of(CustomValue).and(having_attributes(value: option_b.id.to_s,
+ custom_field_id: custom_field.id))
+ ])
+ end
+ end
+
+ context 'with a multi-value list custom field with default value of multiple options' do
+ let(:custom_field) { create(:wp_custom_field, :multi_list, default_options: ['D', 'B', 'F']) }
+
+ it 'returns an array with CustomValues whose values are the stringified ids of the default custom options' do
+ option_d = custom_field.custom_options.find_by(value: 'D')
+ option_b = custom_field.custom_options.find_by(value: 'B')
+ option_f = custom_field.custom_options.find_by(value: 'F')
+ expect(work_package.custom_field_values)
+ .to match([
+ an_instance_of(CustomValue).and(having_attributes(value: option_b.id.to_s,
+ custom_field_id: custom_field.id)),
+ an_instance_of(CustomValue).and(having_attributes(value: option_d.id.to_s,
+ custom_field_id: custom_field.id)),
+ an_instance_of(CustomValue).and(having_attributes(value: option_f.id.to_s,
+ custom_field_id: custom_field.id))
+ ])
+ end
+ end
+ end
+
describe '#custom_field_:id' do
let(:included_cf) { build(:work_package_custom_field) }
let(:other_cf) { build(:work_package_custom_field) }