Skip to content

Instantly share code, notes, and snippets.

@phoffer phoffer/dark_launch.rb
Last active Aug 29, 2015

Embed
What would you like to do?
dark launch with mixin
module DarkLaunch
FEATURES = %w(feature_a feature_b feature_c) # could load this from a config file
def self.included(base)
FEATURES.each do |f|
base.send(:define_method, "has_access_to_#{f}?") { DarkLaunch.feature_visible(f, self) }
end
# could add a method_missing handler for the same prefix, for things that aren't explicitly defined
end
def self.feature_visible(feature_id, current_user)
if !current_user
# Only logged in users should have access to Dark Launch features
return false
end
if Rails.env.development? || Rails.env.test?
# On Dev and Test these features should always be visible (for now at least)
return true
end
# We're prefixing the ENV var ourselves with 'FEATURE_'
# this can be be loaded from anywhere, if you don't like environment variables
env_var = ENV["FEATURE_#{feature_id}"]
if env_var
# If it equals the string "PUBLIC" then return true and don't check invidiual user_ids anymore
return true if env_var == "PUBLIC"
# If we've got an array then we'll check whether the user ID is in there
# Note: this is the actual piece of logic that allows us to control who to launch a feature to. Right now we're basing this
# off of customer IDs. This could as well be 10% of our users, traffic from a certain country, only users with an email
# of ...@my_own_company.com or whatever.
return env_var.split(',').include?(current_user.id.to_s) unless !env_var
end
# Apparently nothing succeeded before so let's be safe and return false for now
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.