Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature flag support using Flipper #387

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ gem 'devise', '~> 4.7', '>= 4.7.2'
gem 'devise_token_auth', '~> 1.2', git: 'https://github.com/lynndylanhurley/devise_token_auth'
gem 'draper', '~> 4.0', '>= 4.0.1'
gem 'exception_hunter', github: 'rootstrap/exception_hunter'
gem 'flipper', '~> 0.28.0'
gem 'flipper-active_record', '~> 0.28.0'
gem 'flipper-ui', '~> 0.28.0'
gem 'jbuilder', '~> 2.10'
gem 'oj', '~> 3.9', '>= 3.9.2'
gem 'pagy', '~> 4.0'
Expand Down
21 changes: 19 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ GEM
faker (2.23.0)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
flipper (0.28.0)
concurrent-ruby (< 2)
flipper-active_record (0.28.0)
activerecord (>= 4.2, < 8)
flipper (~> 0.28.0)
flipper-ui (0.28.0)
erubi (>= 1.0.0, < 2.0.0)
flipper (~> 0.28.0)
rack (>= 1.4, < 3)
rack-protection (>= 1.5.3, <= 4.0.0)
sanitize (< 7)
font-awesome-sass (5.15.1)
sassc (>= 1.11)
formtastic (4.0.0)
Expand Down Expand Up @@ -296,6 +307,8 @@ GEM
rack (2.2.6.4)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-protection (3.0.6)
rack
rack-test (2.0.2)
rack (>= 1.3)
rails (7.0.4.3)
Expand Down Expand Up @@ -401,6 +414,9 @@ GEM
rubocop (>= 0.72, < 2)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
sanitize (6.0.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
sassc (2.4.0)
Expand Down Expand Up @@ -437,8 +453,6 @@ GEM
timeout (0.3.2)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2023.3)
tzinfo (>= 1.0.0)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.8.0)
Expand Down Expand Up @@ -479,6 +493,9 @@ DEPENDENCIES
exception_hunter!
factory_bot_rails (~> 5.1, >= 5.1.1)
faker (~> 2.13)
flipper (~> 0.28.0)
flipper-active_record (~> 0.28.0)
flipper-ui (~> 0.28.0)
i18n-tasks (~> 0.9.30)
jbuilder (~> 2.10)
letter_opener (~> 1.7)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This template comes with:
- Reset password
- Get and update user profile
- Administration panel for users
- Feature flags support with a UI for management.
- RSpec tests
- Code quality tools
- API documentation following https://apiblueprint.org/
Expand Down Expand Up @@ -79,6 +80,7 @@ To illustrate, `bin/rails console` will run the console in the docker container
- [ExceptionHunter](https://github.com/rootstrap/exception_hunter) for exception tracking
- [Factory Bot](https://github.com/thoughtbot/factory_bot) for testing data
- [Faker](https://github.com/stympy/faker) for generating test data
- [Flipper](https://github.com/jnunemaker/flipper) for feature flag support
- [Jbuilder](https://github.com/rails/jbuilder) for json views
- [Letter Opener](https://github.com/ryanb/letter_opener) for previewing a mail in the browser
- [Oj](https://github.com/ohler55/oj) for optimized json
Expand Down
12 changes: 6 additions & 6 deletions config/initializers/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@
#
# If you wanted to add a static menu item to the default menu provided:
#
# config.namespace :admin do |admin|
# admin.build_menu :default do |menu|
# menu.add label: "My Great Website",
# url: "http://www.mygreatwebsite.com", html_options: { target: :blank }
# end
# end
config.namespace :admin do |admin|
admin.build_menu :default do |menu|
menu.add label: 'Feature Flags',
url: '/admin/feature-flags', html_options: { target: :blank }
end
end
Comment on lines +232 to +237
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds the feature flag menu item to the active_admin page


# == Download Links
#
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :admin do
authenticate(:admin_user) do
santib marked this conversation as resolved.
Show resolved Hide resolved
mount Flipper::UI.app(Flipper) => '/feature-flags'
end
end
ExceptionHunter.routes(self)
mount_devise_token_auth_for 'User', at: '/api/v1/users', controllers: {
registrations: 'api/v1/registrations',
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20230704182448_create_flipper_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateFlipperTables < ActiveRecord::Migration[7.0]
def self.up
create_table :flipper_features do |t|
t.string :key, null: false
t.timestamps null: false
end
add_index :flipper_features, :key, unique: true

create_table :flipper_gates do |t|
t.string :feature_key, null: false
t.string :key, null: false
t.string :value
t.timestamps null: false
end
add_index :flipper_gates, %i[feature_key key value], unique: true
end

def self.down
santib marked this conversation as resolved.
Show resolved Hide resolved
drop_table :flipper_gates
drop_table :flipper_features
end
end
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_09_05_200553) do
ActiveRecord::Schema[7.0].define(version: 2023_07_04_182448) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
Expand Down Expand Up @@ -102,6 +102,22 @@
t.index ["error_group_id"], name: "index_exception_hunter_errors_on_error_group_id"
end

create_table "flipper_features", force: :cascade do |t|
t.string "key", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["key"], name: "index_flipper_features_on_key", unique: true
end

create_table "flipper_gates", force: :cascade do |t|
t.string "feature_key", null: false
t.string "key", null: false
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
end

create_table "settings", force: :cascade do |t|
t.string "key", null: false
t.string "value"
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
RSpec.configure do |config|
config.render_views = true
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :request
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adds devise helpers like sign_in to requests tests

config.include ActiveJob::TestHelper
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
Expand Down
33 changes: 33 additions & 0 deletions spec/requests/api/v1/feature_flags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe 'GET admin/feature-flags', type: :request do
subject { get '/admin/feature-flags' }

context 'with an admin_user account' do
let(:user) { create :admin_user }

context 'with a valid session' do
before { sign_in user }

it 'shows the feature flags page' do
is_expected.to redirect_to('/admin/feature-flags/features')
end
end

context 'without a valid session' do
it 'redirects the user to the admin login page' do
is_expected.to redirect_to('/admin/login')
end
end
end

context 'with a user account' do
let(:user) { create :user }

context 'with a valid session' do
before { sign_in user }

it 'blocks user access to the feature flags page' do
is_expected.not_to redirect_to('/admin/feature-flags/features')
end
end
end
end