Cookie based feature flags implementation for Rails.
Add this line to your application's Gemfile:
gem 'cookie_flag'
And then execute:
$ bundle
$ bundle exec rails generate cookie_flag:install
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
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
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 %>
Bug reports and pull requests are welcome on GitHub at https://github.com/iguchi1124/cookie_flag.
The gem is available as open source under the terms of the MIT License.