Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pcushing/9bab4662fad1d9b2ac76 to your computer and use it in GitHub Desktop.
Save Pcushing/9bab4662fad1d9b2ac76 to your computer and use it in GitHub Desktop.
Launch Darkly in Rails App
# 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