Giter Site home page Giter Site logo

flip's Introduction

Flip — flip your features

Build Status

Flip provides a declarative, layered way of enabling and disabling application functionality at run-time.

This gem optimizes for:

  • developer ease-of-use,
  • visibility and control for other stakeholders (like marketing); and
  • run-time performance

There are three layers of strategies per feature:

  • default
  • database, to flip features site-wide for all users
  • cookie, to flip features just for you (or someone else)

There is also a configurable system-wide default - !Rails.env.production?` works nicely.

Flip has a dashboard UI that's easy to understand and use.

Feature Flipper Dashboard

Install

Rails 3.0, 3.1 and 3.2+

# Gemfile
gem "flip"

# Generate the model and migration
> rails g flip:install

# Run the migration
> rake db:migrate

# Include the Feature model, e.g. config/initializers/feature.rb:
require 'feature'

Declaring Features

# This is the model class generated by rails g flip:install
class Feature < ActiveRecord::Base
  extend Flip::Declarable

  # The recommended Flip strategy stack.
  strategy Flip::CookieStrategy
  strategy Flip::DatabaseStrategy
  strategy Flip::DefaultStrategy
  default false

  # A basic feature declaration.
  feature :shiny_things

  # Override the system-wide default.
  feature :world_domination, default: true

  # Enabled half the time..? Sure, we can do that.
  feature :flakey,
    default: proc { rand(2).zero? }

  # Provide a description, normally derived from the feature name.
  feature :something,
    default: true,
    description: "Ability to purchase enrollments in courses"

end

Checking Features

Flip.on? or the dynamic predicate methods are used to check feature state:

Flip.on? :world_domination   # true
Flip.world_domination?       # true

Flip.on? :shiny_things       # false
Flip.shiny_things?           # false

Views and controllers use the feature?(key) method:

<div>
  <% if feature? :world_domination %>
    <%= link_to "Dominate World", world_dominations_path %>
  <% end %>
</div>

Feature Flipping Controllers

The Flip::ControllerFilters module is mixed into the base ApplicationController class. The following controller will respond with 404 Page Not Found to all but the index action unless the :something feature is enabled:

class SampleController < ApplicationController

  require_feature :something, :except => :index

  def show
  end

  def index
  end

end

Dashboard

The dashboard provides visibility and control over the features.

The gem includes some basic styles:

= content_for :stylesheets_head do
  = stylesheet_link_tag "flip"

You probably don't want the dashboard to be public. Here's one way of implementing access control.

app/controllers/admin/features_controller.rb:

class Admin::FeaturesController < Flip::FeaturesController
  before_action :assert_authenticated_as_admin
end

app/controllers/admin/strategies_controller.rb:

class Admin::StrategiesController < Flip::StrategiesController
  before_action :assert_authenticated_as_admin
end

routes.rb:

namespace :admin do
  resources :features, only: [ :index ] do
    resources :strategies, only: [ :update, :destroy ]
  end
end

mount Flip::Engine => "/admin/features"

Cacheable

You can optimize your feature to ensure that it doesn't make a ton of feature calls by adding Cacheable to your model.

extend Flip::Cacheable

This will ensure that your features are eager loaded with one call to the database instead of every call to Flip#on? generating a call to the database. This is helpful if you have a larger Rails application and more than a few features defined.

To start or reset the cache, just call #start_feature_cache.


Created by Paul Annesley Copyright © 2011-2013 Learnable Pty Ltd, MIT Licence.

flip's People

Contributors

pda avatar lenidot avatar olleolleolle avatar sherin avatar bsharpe avatar joefiorini avatar pewniak747 avatar ryanhouston avatar kwent avatar piotrze avatar pfac avatar montanalow avatar mwitek avatar kcm avatar jcoyne avatar jasonblalock avatar carloslopes avatar bak avatar acoravos avatar

Stargazers

Cynthia Rattey avatar

Watchers

jack dempsey avatar Fahim Ferdous avatar Reid van Melle avatar Erik Berlin avatar Moses Lee avatar Sean Cashin avatar Jonathan Phillips avatar Blaine Schanfeldt avatar Tim Marshall avatar Michael Scheibe avatar Jade Dominguez avatar Christopher Cope avatar Matt Ruten avatar Kacy Fortner avatar Arnaud Ferreri avatar Ed Carrel avatar Lucas Rosa Galego avatar Diego Maniloff avatar Aamir Poonawalla avatar Jason Dusek avatar Tyler Tate avatar Mostafa Abdelraouf avatar Ryan Dick avatar Michael Amor Righi avatar  avatar Maksim Golivkin avatar Alex Charlton avatar Joey Dong avatar Pedro Assis avatar Tim Barnes avatar Roberto Medri avatar Shane avatar Rahil Sondhi avatar Thomas Loh avatar Jon Hsieh avatar Shrikar Archak avatar James Cloos avatar Alex Gabriel avatar  avatar Hetong Li avatar Matthew Hui avatar  avatar Bryan Rusk avatar Christopher Jenkins avatar Jason Lew avatar Spencer Schack avatar Richard King avatar François Blavoet avatar zain avatar  avatar Dominic Cocchiarella avatar Tomoaki Tsuruta avatar Teodor Lefter avatar Richard Roberts avatar Francisco Guzman avatar Christopher Gregorio avatar Nicholas Wargnier avatar Austin Pivarnik avatar Damon Chin avatar Nicolas avatar Jeremy Stanley avatar Deepak Tirumalasetty avatar  avatar Laimonas Turauskas avatar Charles Wesley avatar  avatar  avatar Richard Gong avatar Nicholas Dujay avatar  avatar Bobby Dennett avatar Brett Shollenberger avatar Helen Kuo avatar Michal Palczewski avatar GregSQL avatar Warren Ng avatar Ted Cheng avatar Howard Fong avatar Than Jianrungsang avatar Sahib Jaspal avatar Ersin Akinci avatar Albert Deng avatar Henry Levin avatar Houtao Deng avatar xianyunmao avatar Tahir Mobashir avatar Scott Jason avatar Eddie Kaiger avatar Bonnie Hoang avatar Link Doucedame avatar Marco Montagna avatar Papa Bosu avatar Anish Kelkar avatar Iris avatar James Jalandoni avatar  Austin Bourdier avatar Martin Ickowicz avatar  avatar Stefanie Snow avatar Ganesh Krishnan avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.