0.01
No release in over 3 years
Low commit activity in last 3 years
Cookie based feature flags implementation for Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

CookieFlag

Cookie based feature flags implementation for Rails.

Installation

Add this line to your application's Gemfile:

gem 'cookie_flag'

And then execute:

$ bundle
$ bundle exec rails generate cookie_flag:install

Usage

At first, edit your config/cookie_flag.yml and configure your feature flag name and feature flag secret.

default: &default
  new_feature: '1'

development:
  <<: *default

test:
  <<: *default

production:
  new_feature: <%= ENV['NEW_FEATURE_SECRET'] %>

then, you can use cookie based feature flag API at controllers and views.

If you want to enable or disable features from admin page, you can use console web.

Edit your config/routes.rb

Rails.application.routes.draw do
  mount CookieFlag::Console, at: "/cookie_flag" if Rails.env.development?
end

Controllers

Use feature class method at controller, you can return 404 response at disabled feature endpoints.

class YourFeaturesController < ApplicationController
  feature :new_feature

  def index
  end
end

And some helper methods feature_available? and feature_flags are available.

class NewFeaturesController < ApplicationController
  def index
    unless feature_available?(:new_feature)
      redirect_to root_path
    end
  end
end

Views

feature_available? helper method is available.

<% if feature_available?(:new_feature) %>
  <p>you can use new feature!</p>
<% else %>
  <p>you cannot use new feature!</p>
<% end %>

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/iguchi1124/cookie_flag.

License

The gem is available as open source under the terms of the MIT License.