Skip to content

Instantly share code, notes, and snippets.

@hendrikbeck
Last active August 29, 2015 14:09
Show Gist options
  • Save hendrikbeck/c942a62980c3bcd91476 to your computer and use it in GitHub Desktop.
Save hendrikbeck/c942a62980c3bcd91476 to your computer and use it in GitHub Desktop.
DIY feature toggle to soft launch and dark launch features in Rails, based on Heroku config vars
class DarkLaunch
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_'
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
- if DarkLaunch.feature_visible('MY_NEW_FEATURE', current_user)
.alert
Hey, we're visible!
- else
.alert
There will be something visible soon here!
@AndyScherzinger
Copy link

I would have expected env_var == "PUBLIC" to mean PUBLIC and thus enabling a feature even if the users isn't logged in, thus this would also work as a general switch not limited to dark launch ;)
This way you could also publicly launch a feature without a redeployment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment