Supercharge Your Feature Toggles with the Flipper Gem 🔄✨
Hey Ruby Enthusiasts! 🎉
Ever wanted to control your app's features like a boss? Imagine flipping switches to turn features on and off without breaking a sweat. Today, we’re diving into the magical world of the flipper gem – your new best friend for feature flagging in Ruby on Rails. Get ready to flip those features like a pro and impress your team with seamless rollouts! 🚀
What is flipper?
Flipper is like having a magical control panel for your Rails app. It lets you enable, disable, and manage feature toggles effortlessly. Whether you’re deploying new features gradually or running experiments, flipper makes it easy and fun!
Why Choose flipper?
Installation and Setup
gem 'flipper'
gem 'flipper-active_record' # if you want to use ActiveRecord as the adapter
2. Bundle Install:
bundle install
3. Generate the Migration:
rails generate flipper:active_record
rails db:migrate
4. Configure Flipper:
# config/initializers/flipper.rb
require 'flipper'
require 'flipper/adapters/active_record'
Flipper.configure do |config|
config.default { Flipper.new(Flipper::Adapters::ActiveRecord.new) }
end
Basic Usage
Defining a Feature:
Think of it as flipping a switch!
Flipper[:new_feature].enable # Turn it on!
Flipper[:new_feature].disable # Turn it off!
Checking a Feature:
Add a bit of magic to your logic.
if Flipper.enabled?(:new_feature)
# Execute code for the new feature
puts "New feature is ON!"
else
# Fallback code
puts "New feature is OFF!"
end
Example Usage
Recommended by LinkedIn
Controller Example:
Toggle features in your controller like a wizard.
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
if Flipper.enabled?(:new_dashboard)
render 'new_dashboard'
else
render 'old_dashboard'
end
end
end
View Example:
Show off your toggling skills in views.
<!-- app/views/users/index.html.erb -->
<% if Flipper.enabled?(:new_dashboard) %>
<%= render 'new_dashboard' %>
<p>Welcome to the NEW dashboard!</p>
<% else %>
<%= render 'old_dashboard' %>
<p>Stuck with the old dashboard...</p>
<% end %>
Advanced Features
Conditional Enabling:
Give your features some special treatment.
Flipper[:new_feature].enable Actor.new(current_user) # Only for special users!
Flipper[:new_feature].enable_group :admins # Only for the cool admins
Percentage of Time:
Play with time and randomness.
Flipper[:new_feature].enable_percentage_of_time 50 # 50% chance it’s ON
Percentage of Actors:
Make some users feel extra special.
Flipper[:new_feature].enable_percentage_of_actors 10 # 10% of users get it
Management UI
Flipper UI:
Manage your features like a DJ with a mixing console.
# config/routes.rb
Rails.application.routes.draw do
mount Flipper::UI.app(Flipper) => '/flipper'
end
Conclusion
The flipper gem is your magic wand for managing feature toggles in Ruby on Rails applications. Its flexibility, ease of use, and powerful control options make it an excellent choice for any Rails developer. Give flipper a try in your next project and start flipping those switches with confidence!
Have you used flipper before? Share your magical experiences and any wizardly tips in the comments!
#RubyOnRails #RailsXray #Flipper #FeatureToggles #WebDevelopment #CodingTips #DeveloperLife #TechFun