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

bail when trying to write a configuration & deprecte non bool setting_name? access

ulfertsf4c4544f7ac6
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 8a7d54c1a84..07861d213d7 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -90,6 +90,12 @@ class Setting < ApplicationRecord
def self.#{name}?
# when running too early, there is no settings table. do nothing
return unless settings_table_exists_yet?
+ definition = Settings::Definition[:#{name}]
+
+ if definition.format != :boolean
+ ActiveSupport::Deprecation.warn "Calling #{self}.#{name}? is deprecated since it is not a boolean", caller
+ end
+
value = self[:#{name}]
ActiveRecord::Type::Boolean.new.cast(value)
end
diff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rb
index 2b1da811258..6b603f040ac 100644
--- a/lib/open_project/configuration.rb
+++ b/lib/open_project/configuration.rb
@@ -191,28 +191,33 @@ module OpenProject
end
def method_missing(name, *args, &block)
- setting_name = name.to_s.sub(/(=|\?)$/, '')
+ setting_name = name.to_s.sub(/\?$/, '')
- if Settings::Definition.exists?(setting_name)
- define_config_methods(setting_name)
+ definition = Settings::Definition[setting_name]
- send(setting_name, *args, &block)
+ if definition
+ define_config_methods(definition)
+
+ send(name, *args, &block)
else
super
end
end
def respond_to_missing?(name, include_private = false)
- Settings::Definition.exists?(name.to_s.sub(/(=|\?)$/, '')) || super
+ Settings::Definition.exists?(name.to_s.sub(/\?$/, '')) || super
end
- def define_config_methods(setting_name)
- define_singleton_method setting_name do
- self[setting_name]
+ def define_config_methods(definition)
+ define_singleton_method definition.name do
+ self[definition.name]
end
- define_singleton_method "#{setting_name}?" do
- ['true', true, '1'].include? self[setting_name]
+ define_singleton_method "#{definition.name}?" do
+ if definition.format != :boolean
+ ActiveSupport::Deprecation.warn "Calling #{self}.#{definition.name}? is deprecated since it is not a boolean", caller
+ end
+ ['true', true, '1'].include? self[definition.name]
end
end
diff --git a/spec/lib/open_project/configuration_spec.rb b/spec/lib/open_project/configuration_spec.rb
index 6df09841415..3da6c540a60 100644
--- a/spec/lib/open_project/configuration_spec.rb
+++ b/spec/lib/open_project/configuration_spec.rb
@@ -41,6 +41,32 @@ describe OpenProject::Configuration do
Setting.clear_cache
end
+ describe '.[setting]' do
+ it 'fetches the value' do
+ expect(described_class.app_title)
+ .to eql('OpenProject')
+ end
+ end
+
+ describe '.[setting]?' do
+ it 'fetches the value' do
+ expect(described_class.smtp_enable_starttls_auto?)
+ .to be false
+ end
+
+ it 'works for non boolean settings as well (deprecated)' do
+ expect(described_class.app_title?)
+ .to be false
+ end
+ end
+
+ describe '.[setting]=' do
+ it 'raises an error' do
+ expect { described_class.smtp_enable_starttls_auto = true }
+ .to raise_error NoMethodError
+ end
+ end
+
describe '.migrate_mailer_configuration!' do
before do
allow(Setting)
diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb
index 777a7e58443..ae94e99b59c 100644
--- a/spec/models/setting_spec.rb
+++ b/spec/models/setting_spec.rb
@@ -33,7 +33,6 @@ describe Setting, type: :model do
described_class.destroy_all
end
- # OpenProject specific defaults that are set in settings.yml
describe "OpenProject's default settings" do
it 'has OpenProject as application title' do
expect(described_class.app_title).to eq 'OpenProject'
@@ -103,6 +102,34 @@ describe Setting, type: :model do
end
end
+ describe '.[setting]' do
+ it 'fetches the value' do
+ expect(described_class.app_title)
+ .to eql('OpenProject')
+ end
+ end
+
+ describe '.[setting]?' do
+ it 'fetches the value' do
+ expect(described_class.smtp_enable_starttls_auto?)
+ .to be false
+ end
+
+ it 'works for non boolean settings as well (deprecated)' do
+ expect(described_class.app_title?)
+ .to be true
+ end
+ end
+
+ describe '.[setting]=' do
+ it 'sets the value' do
+ described_class.app_title = 'New title'
+
+ expect(described_class.app_title)
+ .to eql('New title')
+ end
+ end
+
describe '.[setting]_writable?' do
before do
allow(Settings::Definition[:host_name])