diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /lib/gitlab/fake_application_settings.rb | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'lib/gitlab/fake_application_settings.rb')
-rw-r--r-- | lib/gitlab/fake_application_settings.rb | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/lib/gitlab/fake_application_settings.rb b/lib/gitlab/fake_application_settings.rb index 211c0967f89..0b9a4c161ae 100644 --- a/lib/gitlab/fake_application_settings.rb +++ b/lib/gitlab/fake_application_settings.rb @@ -1,35 +1,51 @@ # frozen_string_literal: true -# This class extends an OpenStruct object by adding predicate methods to mimic +# Fakes ActiveRecord attribute storage by adding predicate methods to mimic # 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 # column type without parsing db/structure.sql. module Gitlab - class FakeApplicationSettings < OpenStruct - include ApplicationSettingImplementation - - # Mimic ActiveRecord predicate methods for boolean values - def self.define_predicate_methods(options) - options.each do |key, value| - next if key.to_s.end_with?('?') - next unless [true, false].include?(value) - - define_method "#{key}?" do - actual_key = key.to_s.chomp('?') - self[actual_key] + 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 end end end - def initialize(options = {}) - super + def initialize(settings = {}) + @table = settings.dup - FakeApplicationSettings.define_predicate_methods(options) + FakeApplicationSettings.define_properties(settings) end - alias_method :read_attribute, :[] - alias_method :has_attribute?, :[] + 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 end end |