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

shift overriding setting values to when a setting is defined

ulfertsa734ed8b8057
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 9ccbe8abc1f..0090360ce34 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -133,11 +133,15 @@ module Settings
@by_name = nil
- all << new(name,
- format: format,
- value: value,
- writable: writable,
- allowed: allowed)
+ definition = new(name,
+ format: format,
+ value: value,
+ writable: writable,
+ allowed: allowed)
+
+ override_value(definition)
+
+ all << definition
end
def define(&block)
@@ -164,10 +168,6 @@ module Settings
unless loaded
self.loaded = true
load './config/constants/settings/definitions.rb'
-
- # The test setup should govern the configuration
- load_config_from_file unless Rails.env.test?
- override_config
end
@all
@@ -180,60 +180,75 @@ module Settings
@all = nil
@loaded = false
@by_name = nil
+ @file_config = nil
end
def by_name
@by_name ||= all.group_by(&:name).transform_values(&:first)
end
- def load_config_from_file
- filename = Rails.root.join('config/configuration.yml')
+ def file_config
+ @file_config ||= begin
+ filename = Rails.root.join('config/configuration.yml')
+
+ file_config = {}
- if File.file?(filename)
- file_config = YAML.load_file(filename)
+ if File.file?(filename)
+ file_config = YAML.load_file(filename)
- if file_config.is_a? Hash
- load_env_from_config(file_config, Rails.env)
- else
- warn "#{filename} is not a valid OpenProject configuration file, ignoring."
+ if file_config.is_a? Hash
+ file_config
+ else
+ warn "#{filename} is not a valid OpenProject configuration file, ignoring."
+ end
end
+
+ file_config
end
end
- def load_env_from_config(config, env)
- config['default']&.each do |name, value|
- self[name]&.override_value(value)
- end
- config[env]&.each do |name, value|
- self[name]&.override_value(value)
+ # Replace values for which an entry in the config file or as an environment variable exists.
+ def override_value(definition)
+ # The test setup should govern the configuration
+ override_value_from_file(definition) unless Rails.env.test?
+ override_value_from_env(definition)
+ end
+
+ def override_value_from_file(definition)
+ name = definition.name
+
+ ['default', Rails.env].each do |env|
+ next unless file_config.dig(env, name)
+
+ definition.override_value(file_config.dig(env, name))
end
end
- # Replace config values for which an environment variable with the same key in upper case
- # exists.
+ # Replace values for which an environment variable with the same key in upper case exists.
# Also merges the existing values that are hashes with values from ENV if they follow the naming
# schema.
- def override_config
- override_config_values
- merge_hash_config
+ def override_value_from_env(definition)
+ override_config_values(definition)
+ merge_hash_config(definition) if definition.format == :hash
end
- def override_config_values
- all
- .map(&:name)
- .select { |key| ENV.include? key.upcase }
- .each { |key| self[key].override_value(extract_value(key, ENV[key.upcase])) }
+ def override_config_values(definition)
+ value = ENV[env_name(definition)]
+
+ return unless value
+
+ definition.override_value(extract_value(definition.name.upcase, value))
end
- def merge_hash_config
- ENV.select { |k, _| k =~ /^#{ENV_PREFIX}/i }.each do |k, raw_value|
- name, value = path_to_hash(*path(ENV_PREFIX, k),
- extract_value(k, raw_value))
- .first
+ def merge_hash_config(definition)
+ ENV.select { |k, _| k =~ /^#{env_name(definition)}/i }.each do |k, raw_value|
+ _, value = path_to_hash(*path(ENV_PREFIX, k),
+ extract_value(k, raw_value))
+ .first
# There might be ENV vars that match the OPENPROJECT_ prefix but are no OP instance
# settings, e.g. OPENPROJECT_DISABLE_DEV_ASSET_PROXY
- self[name]&.override_value(value)
+ definition.override_value(value)
end
end
@@ -250,7 +265,6 @@ module Settings
# where the last parameter becomes the value.
#
# e.g. path_to_hash(:a, :b, :c, :d) => { a: { b: { c: :d } } }
-
def path_to_hash(*path)
value = path.pop
@@ -263,6 +277,10 @@ module Settings
path_segment.gsub '__', '_'
end
+ def env_name(definition)
+ "#{ENV_PREFIX}#{definition.name.upcase.gsub('_', '__')}"
+ end
+
##
# Extract the configuration value from the given input
# using YAML.
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index ff95b1ac972..38f25c84a48 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -124,6 +124,18 @@ describe Settings::Definition do
expect(all.detect { |d| d.name == 'bogus' })
.to be_nil
end
+
+ it 'ENV vars for which a definition has been added after #all was called first (e.g. in a module)' do
+ stub_const('ENV', { 'OPENPROJECT_BOGUS' => '1' })
+
+ all
+
+ described_class.add 'bogus',
+ value: 0
+
+ expect(all.detect { |d| d.name == 'bogus' }.value)
+ .to eq 1
+ end
end
context 'when overriding from file' do
@@ -149,11 +161,6 @@ describe Settings::Definition do
.with(Rails.root.join('config/configuration.yml'))
.and_return(file_contents)
- allow(YAML)
- .to receive(:load_file)
- .with(Rails.root.join('config/settings.yml'))
- .and_return({})
-
allow(File)
.to receive(:file?)
.with(Rails.root.join('config/configuration.yml'))
diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb
index 1446055570d..777a7e58443 100644
--- a/spec/models/setting_spec.rb
+++ b/spec/models/setting_spec.rb
@@ -50,7 +50,7 @@ describe Setting, type: :model do
# checks whether settings can be set and are persisted in the database
describe 'changing a setting' do
- context "setting doesn't exist in the database" do
+ context "for a setting that doesn't exist in the database" do
before do
described_class.host_name = 'some name'
end
@@ -83,7 +83,7 @@ describe Setting, type: :model do
end
end
- context 'setting already exist in the database' do
+ context 'for a setting that already exist in the database' do
before do
described_class.host_name = 'some name'
described_class.host_name = 'some other name'