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

Can use space-separated string for array settings

Christophe Bliardc9ed88d3ff41
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index e9bc34a585b..fef3c36acda 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -1084,18 +1084,9 @@ module Settings
end
def override_value(other_value)
- if format == :hash
- self.value = {} if value.nil?
- value.deep_merge! other_value.deep_stringify_keys
- elsif format == :datetime && !other_value.is_a?(DateTime)
- self.value = DateTime.parse(other_value.to_s)
- else
- self.value = other_value
- end
-
- raise ArgumentError, "Value for #{name} must be one of #{allowed.join(', ')} but is #{value}" unless valid?
-
+ self.value = coerce(other_value, format)
self.writable = false
+ raise ArgumentError, "Value for #{name} must be one of #{allowed.join(', ')} but is #{value}" unless valid?
end
def valid?
@@ -1398,5 +1389,18 @@ module Settings
raise ArgumentError, "Cannot deduce the format for the setting definition #{name}"
end
end
+
+ def coerce(value, format)
+ case format
+ when :hash
+ (self.value || {}).deep_merge value.deep_stringify_keys
+ when :array
+ value.is_a?(String) ? value.split : Array(value)
+ when :datetime
+ value.is_a?(DateTime) ? value : DateTime.parse(value.to_s)
+ else
+ value
+ end
+ end
end
end
diff --git a/lib_static/open_project/configuration/helpers.rb b/lib_static/open_project/configuration/helpers.rb
index f6fdad3e118..528bfa7e387 100644
--- a/lib_static/open_project/configuration/helpers.rb
+++ b/lib_static/open_project/configuration/helpers.rb
@@ -135,14 +135,6 @@ module OpenProject
menus.to_h
end
- def disabled_modules
- array self['disabled_modules']
- end
-
- def blacklisted_routes
- array self['blacklisted_routes']
- end
-
##
# Whether we're running a bim edition
def bim?
@@ -199,7 +191,7 @@ module OpenProject
# In the latter case the string will be split and the values returned as an array.
def array(value)
if value.is_a?(String) && value =~ / /
- value.split ' '
+ value.split
else
Array(value)
end
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index e9ca0e23056..7a856fc823a 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -308,12 +308,25 @@ describe Settings::Definition, :settings_reset do
)
reset(:blacklisted_routes)
- # works for OpenProject::Configuration thanks to OpenProject::Configuration::Helper mixin
expect(OpenProject::Configuration.blacklisted_routes)
.to eq(['admin/info', 'admin/plugins'])
- # sadly behaves differently for Setting
expect(Setting.blacklisted_routes)
- .to eq('admin/info admin/plugins')
+ .to eq(['admin/info', 'admin/plugins'])
+ end
+
+ it 'allows overriding configuration array from ENV with single string' do
+ stub_const(
+ 'ENV',
+ {
+ 'OPENPROJECT_DISABLED__MODULES' => 'repository'
+ }
+ )
+
+ reset(:disabled_modules)
+ expect(OpenProject::Configuration.disabled_modules)
+ .to eq(['repository'])
+ expect(Setting.disabled_modules)
+ .to eq(['repository'])
end
context 'with definitions from plugins' do
@@ -325,7 +338,7 @@ describe Settings::Definition, :settings_reset do
'OPENPROJECT_2FA_ALLOW__REMEMBER__FOR__DAYS' => '15'
}
)
- # override from env manually because this settings are added by plugin intself
+ # override from env manually because these settings are added by plugin itself
described_class.send(:override_value, all[:plugin_openproject_two_factor_authentication])
expect(all[:plugin_openproject_two_factor_authentication].value).to eq('active_strategies' => [:totp],
'enforced' => true, 'allow_remember_for_days' => 15)
@@ -338,7 +351,7 @@ describe Settings::Definition, :settings_reset do
'OPENPROJECT_2FA' => '{"enforced": true, "allow_remember_for_days": 15}'
}
)
- # override from env manually because this settings are added by plugin intself
+ # override from env manually because these settings are added by plugin itself
described_class.send(:override_value, all[:plugin_openproject_two_factor_authentication])
expect(all[:plugin_openproject_two_factor_authentication].value)
.to eq({ 'active_strategies' => [:totp], 'enforced' => true, 'allow_remember_for_days' => 15 })
@@ -365,6 +378,8 @@ describe Settings::Definition, :settings_reset do
edition: 'bim'
sendmail_location: 'default_location'
direct_uploads: false
+ disabled_modules: 'repository'
+ blacklisted_routes: 'admin/info admin/plugins'
test:
smtp_address: 'test address'
sendmail_location: 'test location'
@@ -414,6 +429,13 @@ describe Settings::Definition, :settings_reset do
expect(all[:consent_time].value).to eql DateTime.parse("2222-01-01")
end
+ it 'correctly converts a space separated string into array for array format' do
+ reset(:disabled_modules)
+ expect(all[:disabled_modules].value).to eq ['repository']
+ reset(:blacklisted_routes)
+ expect(all[:blacklisted_routes].value).to eq ['admin/info', 'admin/plugins']
+ end
+
it 'correctly overrides a default by a false value' do
reset(:direct_uploads)
expect(all[:direct_uploads].value).to be false