Skip to content

Instantly share code, notes, and snippets.


Brandon Keepers bkeepers

View GitHub Profile
bkeepers / flipper_controller.rb
Created March 22, 2023 12:47
Simple Flipper API for your app
View flipper_controller.rb
class FlipperController < ApplicationController
# Returns the value of all feature flags for the current user
def index
features = do |feature|
[feature.key, Flipper.enabled?(feature.key, current_user)]
render json: features.to_h
bkeepers / initializer.rb
Created March 14, 2023 20:32
Use Sync adapter to keep memory in sync with storage adapter
View initializer.rb
# config/initializers/flipper.rb
require 'flipper/adapters/sync'
Rails.application.configure do
# Disable per-request memoization
config.flipper.memoize = false
Flipper.configure do |config|
bkeepers / initializer.rb
Last active March 6, 2023 11:32
Use Flipper::Adapters::Poll to keep persisted features synced into memory. Polling happens in a background thread and loads all features into an in-memory adapter.
View initializer.rb
require "flipper/adapters/dual_write"
require "flipper/adapters/poll"
Flipper.configure do |config|
config.adapter do
# Primary storage adapter
storage_adapter =
# Poll the primary storage adapter every 10 seconds to get all features
poller = Flipper::Adapters::Poll::Poller.get('memoizer', {
bkeepers / initializer.rb
Last active February 8, 2023 14:03
Migrate from plain ol' delayed_job to ActiveJob without losing jobs in the queue when you deploy
View initializer.rb
# Allow legacy jobs in the queue to be initialized and processed now that they use ActiveJob
# Each job must also define `#init_with(coder)` to define how serialized instance variables are
# used to initialize the new ActiveJob instance.
module Delayed::LegacyJob
class Proxy < SimpleDelegator
# ActiveJob calls `#perform_now` internally when executing a job to pass arguments to `#perform`
def perform
bkeepers / flipper-bugsnag.rb
Last active December 16, 2022 14:06
Flipper + BugSnag feature flags
View flipper-bugsnag.rb
# config/initializers/flipper.rb
ActiveSupport::Notifications.subscribe('feature_operation.flipper') do |event|
operation, feature_name, result = event.payload.values_at(:operation, :feature_name, :result)
return unless operation == :enabled?
result ? Bugsnag.add_feature_flag(feature_name) : Bugsnag.clear_feature_flag(feature_name)
bkeepers / example.rb
Last active September 14, 2022 13:58
Multivariate - A/B - split testing with Flipper
View example.rb
# Register test groups
Flipper.register(:group_a) do |actor, context|
# Algorithm or database lookup to determine if a user is in this group
# e.g. 10% percentage of actors
crc32('group_a' + context.feature_name + actor.flipper_id) % 100 < 10
Flipper.register(:group_b) do |actor, context|
bkeepers /
Last active October 24, 2022 20:02
Fail Rails system tests if there are any JavaScript errors

We're on the cusp of 2022 and most software is now written in JavaScript, but it is still virtually impossible to fail integration tests on a JavaScript error and get a useful stack trace from the browser.

This gist includes my description of the problem and sample code from my attempts to solve it.

What I want:

  • Integration test should fail (preferabbly immediately) on any uncaught JavaScript error (window.onerror, unhandledrejection, etc)
  • The stack trace should use source maps and be close to what browsers show in DevTools

The tools I'm currently using:

bkeepers / iframe_location_controller.js
Last active March 12, 2021 00:55
Stimulus controller to save location of same-origin iframe in hash of parent window and restore on reload.
View iframe_location_controller.js
import { Controller } from 'stimulus'
// Stimulus controller to save location of same-origin iframe in hash of parent
// window and restore on reload.
// <iframe src="…"
// data-controller="iframe-location"
// data-action="load->iframe-location#save">
// </frame>
bkeepers / data.csv
Last active February 24, 2021 15:25
View data.csv
age length price
36 29 12500
41 38 34900
45 13 1250
11 26 22400
19 48 265000
33 38 89900
5 42 399000
39 28 3200
39 27 14750
bkeepers / application.rb
Created May 13, 2020 15:26
Add methods to `rails console`
View application.rb
module SailboatGuide
class Application < Rails::Application
console do
require 'console_helpers'
TOPLEVEL_BINDING.eval('self').extend ConsoleHelpers