Add simple feature flag system (#34038)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
		@@ -35,6 +35,7 @@ require_relative '../lib/paperclip/type_corrector'
 | 
			
		||||
require_relative '../lib/paperclip/response_with_limit_adapter'
 | 
			
		||||
require_relative '../lib/terrapin/multi_pipe_extensions'
 | 
			
		||||
require_relative '../lib/mastodon/snowflake'
 | 
			
		||||
require_relative '../lib/mastodon/feature'
 | 
			
		||||
require_relative '../lib/mastodon/version'
 | 
			
		||||
require_relative '../lib/mastodon/rack_middleware'
 | 
			
		||||
require_relative '../lib/public_file_server_middleware'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
---
 | 
			
		||||
shared:
 | 
			
		||||
  experimental_features: <%= ENV.fetch('EXPERIMENTAL_FEATURES', nil) %>
 | 
			
		||||
  self_destruct_value: <%= ENV.fetch('SELF_DESTRUCT', nil) %>
 | 
			
		||||
  software_update_url: <%= ENV.fetch('UPDATE_CHECK_URL', 'https://api.joinmastodon.org/update-check') %>
 | 
			
		||||
  source:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								lib/mastodon/feature.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								lib/mastodon/feature.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Mastodon::Feature
 | 
			
		||||
  class << self
 | 
			
		||||
    def enabled_features
 | 
			
		||||
      @enabled_features ||=
 | 
			
		||||
        (Rails.configuration.x.mastodon.experimental_features || '').split(',').map(&:strip)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def method_missing(name)
 | 
			
		||||
      if respond_to_missing?(name)
 | 
			
		||||
        feature = name.to_s.delete_suffix('_enabled?')
 | 
			
		||||
        enabled = enabled_features.include?(feature)
 | 
			
		||||
        define_singleton_method(name) { enabled }
 | 
			
		||||
 | 
			
		||||
        return enabled
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      super
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def respond_to_missing?(name)
 | 
			
		||||
      name.to_s.end_with?('_enabled?')
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										30
									
								
								spec/lib/mastodon/feature_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								spec/lib/mastodon/feature_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe Mastodon::Feature do
 | 
			
		||||
  around do |example|
 | 
			
		||||
    original_value = Rails.configuration.x.mastodon.experimental_features
 | 
			
		||||
    Rails.configuration.x.mastodon.experimental_features = 'fasp,fetch_all_replies'
 | 
			
		||||
    example.run
 | 
			
		||||
    Rails.configuration.x.mastodon.experimental_features = original_value
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe '::fasp_enabled?' do
 | 
			
		||||
    subject { described_class.fasp_enabled? }
 | 
			
		||||
 | 
			
		||||
    it { is_expected.to be true }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe '::fetch_all_replies_enabled?' do
 | 
			
		||||
    subject { described_class.fetch_all_replies_enabled? }
 | 
			
		||||
 | 
			
		||||
    it { is_expected.to be true }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe '::unspecified_feature_enabled?' do
 | 
			
		||||
    subject { described_class.unspecified_feature_enabled? }
 | 
			
		||||
 | 
			
		||||
    it { is_expected.to be false }
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user