Groups
Enable features for a set of actors that share a common trait, which is determined by a configurable block at runtime.
Turn on feature based on the return value of block. Super flexible way to turn on a feature for multiple things (users, people, accounts, etc.) as long as the thing returns true when passed to the block.
Registering a group
Use Flipper.register
in an initializer to register a new group. The block passed to register
will be called with the actor and a feature context when enabled?
is called.
config/initializers/flipper.rb
# Define the block that will return true or false based on actor.
# Registers a group named admins .
Flipper.register(:admins) do |actor, context|
actor.respond_to?(:admin?) && actor.admin?
end
Since Flipper allows any object to be an actor, check that actor.respond_to?(…)
before calling a method on it.
Enabling a feature for a group
A feature can be enabled for a group by calling enable_group
with the name of the feature and the group.
Flipper.enable_group(:documentation, :admins)
Example
When a feature is enabled for a group, calls to Flipper.enabled?
with an actor will check if the actor is in that group.
user = User.where(admin: false).first # user who returns false for admin? method
admin = User.where(admin: true).first # user who returns true for admin? method
# The feature is not enabled for a normal user
Flipper.enabled?(:documentation, user) # false
# The feature is enabled for an admin user.
Flipper.enabled?(:documentation, admin) # true
Get audit history, rollbacks, advanced permissions, analytics, and all of your projects in one place.
You can choose from several tiers to sponsor Flipper on GitHub and get some great benefits!