# This class extends an OpenStruct object 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/schema.rb.
module Gitlab
  class FakeApplicationSettings < OpenStruct
    def initialize(options = {})
      super

      FakeApplicationSettings.define_predicate_methods(options)
    end

    # 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]
        end
      end
    end
  end
end

# This class extends an OpenStruct object 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/schema.rb.
module Gitlab
  class FakeApplicationSettings < OpenStruct
    def initialize(options = {})
      super

      FakeApplicationSettings.define_predicate_methods(options)
    end

    def pick_repository_storage
      repository_storages.sample
    end

    # 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]
        end
      end
    end
  end
end

Work around migration specs failing due to repository storages not being selected
Work around migration specs failing due to repository storages not being selected

Migration specs may be missing a column necessary to create an ApplicationSetting
object, which causes the FakeApplicationSetting to be initialized. However, the
project selects a random repository storage via ApplicationSetting#pick_repository_storage,
which isn't present in the OpenStruct.

To workaround this issues, we add a pick_repository_storage method in
FakeApplicationSetting.
