Last active
August 29, 2015 14:08
-
-
Save Pcushing/9bab4662fad1d9b2ac76 to your computer and use it in GitHub Desktop.
Launch Darkly in Rails App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You may need to add /lib to your autoload paths in /config/application.rb | |
config.autoload_paths += %W(#{config.root}/lib) | |
# /lib/extensions/launch_darkly.rb | |
module Extensions | |
module LaunchDarkly | |
def self.included(c) | |
return unless c | |
c.helper_method :pilot_navbar? # anything that needs view-level control | |
end | |
def ld | |
@ld_client ||= ::LaunchDarkly::LDClient.new(ENV['LAUNCH_DARKLY_API_KEY']) | |
end | |
def pilot_redirect? | |
feature?('inthepilot.redirect') | |
end | |
def pilot_navbar? | |
feature?('inthepilot.navbar') | |
end | |
def feature?(key) | |
return unless current_user # current_user helper defined by Devise gem | |
ld.get_flag?(key, { key: current_user.slug, ip: current_user.account.current_sign_in_ip, custom: { member: current_user.memberships.any? } }, false) | |
end | |
end | |
end | |
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
include Extensions::LaunchDarkly | |
end | |
# where ever we need feature flags in our codebase... | |
def redirects | |
case | |
when pilot_redirect? | |
authenticated_pilot_home | |
when | |
#... | |
else | |
end | |
end | |
# Navbar in our html.haml files | |
- if pilot_navbar? | |
# pilot navbar | |
- else | |
# standard navbar | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment