2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-11 05:10:00 -04:00
|
|
|
# Fakes ActiveRecord attribute storage by adding predicate methods to mimic
|
2017-06-17 10:35:30 -04:00
|
|
|
# ActiveRecord access. We rely on the initial values being true or false to
|
|
|
|
# determine whether to define a predicate method because for a newly-added
|
|
|
|
# column that has not been migrated yet, there is no way to determine the
|
2020-03-22 11:09:49 -04:00
|
|
|
# column type without parsing db/structure.sql.
|
2017-06-17 10:35:30 -04:00
|
|
|
module Gitlab
|
2021-08-11 05:10:00 -04:00
|
|
|
class FakeApplicationSettings
|
|
|
|
prepend ApplicationSettingImplementation
|
|
|
|
|
|
|
|
def self.define_properties(settings)
|
|
|
|
settings.each do |key, value|
|
|
|
|
define_method key do
|
|
|
|
read_attribute(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
if [true, false].include?(value)
|
|
|
|
define_method "#{key}?" do
|
|
|
|
read_attribute(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method "#{key}=" do |v|
|
|
|
|
@table[key.to_sym] = v
|
2017-06-17 10:35:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-11 00:28:03 -04:00
|
|
|
|
2021-08-11 05:10:00 -04:00
|
|
|
def initialize(settings = {})
|
|
|
|
@table = settings.dup
|
2018-09-11 00:28:03 -04:00
|
|
|
|
2021-08-11 05:10:00 -04:00
|
|
|
FakeApplicationSettings.define_properties(settings)
|
2018-09-11 00:28:03 -04:00
|
|
|
end
|
|
|
|
|
2021-08-11 05:10:00 -04:00
|
|
|
def read_attribute(key)
|
|
|
|
@table[key.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_attribute?(key)
|
|
|
|
@table.key?(key.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Mimic behavior of OpenStruct, which absorbs any calls into undefined
|
|
|
|
# properties to return `nil`.
|
|
|
|
def method_missing(*)
|
|
|
|
nil
|
|
|
|
end
|
2017-06-17 10:35:30 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::FakeApplicationSettings.prepend_mod_with('Gitlab::FakeApplicationSettings')
|