Allow settings defaults to be lambdas even if format present

pull/11157/head
Oliver Günther 2 years ago
parent 0cae0ede21
commit 2b05d50b98
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 2
      app/helpers/static_links_helper.rb
  2. 8
      config/constants/settings/definition.rb
  3. 4
      config/constants/settings/definitions.rb
  4. 4
      config/environments/production.rb
  5. 7
      lib_static/open_project/configuration/helpers.rb
  6. 13
      spec/constants/settings/definition_spec.rb

@ -32,7 +32,7 @@ module StaticLinksHelper
def static_link_to(key, label: nil)
item = OpenProject::Static::Links.links.fetch key
link_to label || t(item[:label], default: item[:label]),
link_to label || t(item[:label]),
item[:href],
class: 'openproject--static-link',
target: '_blank', rel: 'noopener'

@ -381,6 +381,8 @@ module Settings
def cast(value)
return nil if value.nil?
value = value.call if value.respond_to?(:call)
case format
when :integer
value.to_i
@ -391,11 +393,7 @@ module Settings
when :symbol
value.to_sym
else
if value.respond_to?(:call)
value.call
else
value
end
value
end
end

@ -668,14 +668,14 @@ Settings::Definition.define do
# Assume we're running in an TLS terminated connection.
add :https,
format: :boolean,
default: Rails.env.production?,
default: -> { Rails.env.production? },
writable: false
# Allow disabling of HSTS headers and http -> https redirects
# for non-localhost hosts
add :hsts,
format: :boolean,
default: -> { https? },
default: true,
writable: false
add :registration_footer,

@ -80,10 +80,10 @@ OpenProject::Application.configure do
config.ssl_options = {
# Disable redirect on the internal SYS API
redirect: {
hsts: OpenProject::Configuration.hsts?,
hsts: OpenProject::Configuration.hsts_enabled?,
exclude: ->(request) do
# Disable redirects when hsts is disabled
return true unless OpenProject::Configuration.hsts?
return true unless OpenProject::Configuration.hsts_enabled?
# Respect the relative URL
relative_url = Regexp.escape(OpenProject::Configuration['rails_relative_url_root'])

@ -38,6 +38,13 @@ module OpenProject
self['direct_uploads']
end
##
# Is hsts really enabled? We only return true
# if the setting is true AND HTTPS mode is on.
def hsts_enabled?
https? && hsts?
end
##
# We only allow direct uploads to S3 as we are using the carrierwave_direct
# gem which only supports S3 for the time being.

@ -956,5 +956,18 @@ describe Settings::Definition do
.to eq 0.5
end
end
context 'with a boolean provided with a proc default' do
let(:instance) do
described_class.new 'bogus',
format: :boolean,
default: -> { false }
end
it 'calls the proc as a default' do
expect(instance.default)
.to be false
end
end
end
end

Loading…
Cancel
Save