DEV Community

Lucas Dachman
Lucas Dachman

Posted on

Self-hosted feature flags for Rails (with typed values)

I built a feature flag gem for Rails that stores flags in your own database. No external service needed.

Why I built this

I wanted feature flags that return more than just true/false. Sometimes you need a number (rate limits), a string (welcome message), or JSON (config object).

Flipper is boolean-only. The bigger tools (LaunchDarkly, etc.) felt like overkill and I didn't want to depend on an external service.

How it works

gem "subflag-rails"
Enter fullscreen mode Exit fullscreen mode
rails generate subflag:install --backend=active_record
rails db:migrate
Enter fullscreen mode Exit fullscreen mode
# config/routes.rb
mount Subflag::Rails::Engine => "/subflag"
Enter fullscreen mode Exit fullscreen mode

That's it. You get an admin UI at /subflag to manage flags.

Admin UI

Usage

# Typed values, not just booleans
limit = subflag_value(:max_uploads, default: 10)
config = subflag_value(:pricing_tiers, default: { free: 5, pro: 50 })
welcome = subflag_value(:welcome_message, default: "Hello")

# Booleans work too
subflag_enabled?(:new_checkout)
Enter fullscreen mode Exit fullscreen mode

User targeting

Show different values to different users:

# config/initializers/subflag.rb
Subflag::Rails.configure do |config|
  config.backend = :active_record

  config.user_context do |user|
    { targeting_key: user.id.to_s, plan: user.plan, email: user.email }
  end
end
Enter fullscreen mode Exit fullscreen mode

Then in your targeting rules, you can say "users with plan=pro get max_uploads=100" while everyone else gets 10.

Cloud option

There's also a hosted version at subflag.com if you want a dashboard, multiple environments, and percentage rollouts. Same API either way.


GitHub: https://github.com/subflag/sdk/tree/main/packages/subflag-rails

Would love feedback if you try it.

Top comments (0)