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

set the default value for newly build custom values

ulfertsbf89e934b364
diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index d0f11347119..aec91ac0fa0 100644
--- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -112,9 +112,9 @@ 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: custom_field, value: nil
- )
+ new_value = custom_values.build(customized: self,
+ custom_field: custom_field,
+ value: custom_field.default_value)
existing_cvs.push new_value
end
@@ -187,6 +187,11 @@ module Redmine
custom_values.each { |cv| cv.destroy unless custom_field_values.include?(cv) }
end
+ # Builds custom values for all custom fields for which no custom value already exists.
+ # The value of that newly build value is set to the default value which can also be nil.
+ # Calling this should only be necessary if additional custom fields are made available
+ # after custom_field_values has already been called as that method will also build custom values
+ # (with their default values set) for all custom values for which no prior value existed.
def set_default_values!
new_values = {}
diff --git a/spec/features/projects/projects_custom_fields_spec.rb b/spec/features/projects/projects_custom_fields_spec.rb
index 3a6b0a94261..f187a670d81 100644
--- a/spec/features/projects/projects_custom_fields_spec.rb
+++ b/spec/features/projects/projects_custom_fields_spec.rb
@@ -28,7 +28,7 @@
require 'spec_helper'
-describe 'Projects custom fields', type: :feature do
+describe 'Projects custom fields', type: :feature, js: true do
shared_let(:current_user) { FactoryBot.create(:admin) }
shared_let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
let(:identifier) { "project_custom_field_values_#{custom_field.id}" }
@@ -42,7 +42,7 @@ describe 'Projects custom fields', type: :feature do
FactoryBot.create(:version_project_custom_field)
end
- scenario 'allows creating a new project (regression #29099)', js: true do
+ scenario 'allows creating a new project (regression #29099)' do
visit new_project_path
fill_in 'project_name', with: 'My project name'
@@ -54,13 +54,55 @@ describe 'Projects custom fields', type: :feature do
end
end
+ describe 'with default values' do
+ let!(:default_int_custom_field) do
+ FactoryBot.create(:int_project_custom_field, default_value: 123)
+ end
+ let!(:default_string_custom_field) do
+ FactoryBot.create(:string_project_custom_field, default_value: 'lorem')
+ end
+ let!(:no_default_string_custom_field) do
+ FactoryBot.create(:string_project_custom_field)
+ end
+
+ scenario 'sets the default values on custom fields and allows overwriting them' do
+ visit new_project_path
+
+ fill_in 'project_name', with: 'My project name'
+ find('.form--fieldset-legend a', text: 'ADVANCED SETTINGS').click
+
+ expect(page)
+ .to have_field default_int_custom_field.name, with: default_int_custom_field.default_value.to_s
+ expect(page)
+ .to have_field default_string_custom_field.name, with: default_string_custom_field.default_value.to_s
+ expect(page)
+ .to have_field no_default_string_custom_field.name, with: nil
+
+ fill_in default_string_custom_field.name, with: 'Overwritten'
+
+ click_on 'Create'
+ expect(page).to have_selector('.flash.notice', text: I18n.t(:notice_successful_create))
+
+ created_project = Project.last
+
+ visit project_settings_project_path(created_project)
+
+ expect(page)
+ .to have_field default_int_custom_field.name, with: default_int_custom_field.default_value.to_s
+ expect(page)
+ .to have_field default_string_custom_field.name, with: 'Overwritten'
+ expect(page)
+ .to have_field no_default_string_custom_field.name, with: nil
+ end
+ end
+
describe 'with long text CF' do
let!(:custom_field) do
FactoryBot.create(:text_project_custom_field)
end
let(:editor) { ::Components::WysiwygEditor.new ".form--field.custom_field_#{custom_field.id}" }
- scenario 'allows settings the project boolean CF (regression #26313)', js: true do
+ scenario 'allows settings the project boolean CF (regression #26313)' do
visit settings_generic_project_path(project.id)
# expect CF, description and status description ckeditor
@@ -87,7 +129,7 @@ describe 'Projects custom fields', type: :feature do
FactoryBot.create(:bool_project_custom_field)
end
- scenario 'allows settings the project boolean CF (regression #26313)', js: true do
+ scenario 'allows settings the project boolean CF (regression #26313)' do
visit settings_generic_project_path(project.id)
expect(page).to have_no_checked_field identifier
check identifier
diff --git a/spec/models/work_package/work_package_custom_fields_spec.rb b/spec/models/work_package/work_package_custom_fields_spec.rb
index 2bd5122045b..284fe15b627 100644
--- a/spec/models/work_package/work_package_custom_fields_spec.rb
+++ b/spec/models/work_package/work_package_custom_fields_spec.rb
@@ -47,12 +47,12 @@ describe WorkPackage, type: :model do
let(:cf_required) { true }
- shared_context 'project with custom field' do
+ shared_context 'project with custom field' do |save = true|
before do
project.work_package_custom_fields << custom_field
type.custom_fields << custom_field
- work_package.save
+ work_package.save if save
end
end
@@ -346,6 +346,34 @@ describe WorkPackage, type: :model do
end
end
+ describe 'default values' do
+ include_context 'project with custom field', false
+
+ context 'for a custom field with default value' do
+ before do
+ custom_field.custom_options[1].update_attribute(:default_value, true)
+ end
+
+ it 'sets the default values for custom_field_values' do
+ expect(work_package.custom_field_values.length)
+ .to eql 1
+
+ expect(work_package.custom_field_values[0].value)
+ .to eql custom_field.custom_options[1].id.to_s
+ end
+ end
+
+ context 'for a custom field without default value' do
+ it 'sets the default values for custom_field_values' do
+ expect(work_package.custom_field_values.length)
+ .to eql 1
+
+ expect(work_package.custom_field_values[0].value)
+ .to be_nil
+ end
+ end
+ end
+
describe 'validation error interpolation' do
let :custom_field do
FactoryBot.create :work_package_custom_field,
diff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rb
index 5f2ab75b8c2..20b644a79c3 100644
--- a/spec/services/projects/set_attributes_service_spec.rb
+++ b/spec/services/projects/set_attributes_service_spec.rb
@@ -120,7 +120,7 @@ describe Projects::SetAttributesService, type: :model do
it 'sets a default identifier' do
allow(Project)
.to receive(:next_identifier)
- .and_return('ipsum')
+ .and_return('ipsum')
expect(subject.result.identifier)
.to eql 'ipsum'
@@ -146,7 +146,7 @@ describe Projects::SetAttributesService, type: :model do
it 'stays nil' do
allow(Project)
.to receive(:next_identifier)
- .and_return('ipsum')
+ .and_return('ipsum')
expect(subject.result.identifier)
.to be_nil
@@ -155,7 +155,7 @@ describe Projects::SetAttributesService, type: :model do
end
end
- context 'public default value', with_settings: {default_projects_public: true} do
+ context 'public default value', with_settings: { default_projects_public: true } do
context 'with a value for is_public provided' do
let(:call_attributes) do
{
@@ -177,7 +177,7 @@ describe Projects::SetAttributesService, type: :model do
end
end
- context 'enabled_module_names default value', with_settings: {default_projects_modules: ['lorem', 'ipsum']} do
+ context 'enabled_module_names default value', with_settings: { default_projects_modules: ['lorem', 'ipsum'] } do
context 'with a value for enabled_module_names provided' do
let(:call_attributes) do
{