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

Refactor Settings::Definition.

Pavel Balashov459fab0d4652
diff --git a/config/application.rb b/config/application.rb
index 8845cacc622..b9ee34140e5 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -215,9 +215,7 @@ module OpenProject
config.active_support.cache_format_version = 7.0
config.after_initialize do
- Settings::Definition::DEFINITIONS.each do |setting_name, setting_options|
- Settings::Definition.add(setting_name, **setting_options)
- end
+ Settings::Definition.add_all
end
def self.root_url
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 9cb01956db7..e9bc34a585b 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -1143,9 +1143,7 @@ module Settings
allowed: nil,
env_alias: nil)
name = name.to_sym
- return if @all.present? && @all[name].present?
-
- @all = {} if @all.nil?
+ return if exists?(name)
definition = new(name,
format:,
@@ -1155,17 +1153,19 @@ module Settings
allowed:,
env_alias:)
override_value(definition)
- @all[name] = definition
+ all[name] = definition
end
- def define(&)
- instance_exec(&)
+ def add_all
+ Settings::Definition::DEFINITIONS.each do |setting_name, setting_options|
+ Settings::Definition.add(setting_name, **setting_options)
+ end
end
def [](name)
name = name.to_sym
- if @all.present? && @all.key?(name)
- @all[name]
+ if exists?(name)
+ all[name]
else
h = DEFINITIONS[name]
add(name, **h) if h.present?
@@ -1173,11 +1173,11 @@ module Settings
end
def exists?(name)
- @all.keys.include?(name.to_sym)
+ all.key?(name.to_sym)
end
def all
- @all
+ @all ||= {}
end
private
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 1306d83104b..e9ca0e23056 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -29,6 +29,27 @@
require 'spec_helper'
describe Settings::Definition, :settings_reset do
+ describe '.add_all' do
+ it 'adds all core setting definitions if they are not loaded' do
+ described_class.instance_variable_set(:@all, nil)
+ expect(described_class.all).to eq({})
+
+ described_class.add_all
+
+ expect(described_class.all.keys).to eq(described_class::DEFINITIONS.keys)
+ end
+
+ it 'does not add any plugin/feature settings if they were removed for some reason' do
+ not_core_settings = described_class.all.keys - described_class::DEFINITIONS.keys
+ expect(not_core_settings).not_to be_empty
+ described_class.instance_variable_set(:@all, nil)
+
+ described_class.add_all
+
+ expect(described_class.all.keys).not_to include(not_core_settings)
+ end
+ end
+
describe '.all' do
subject(:all) { described_class.all }