Skip to content
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 .traceroute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ ignore_unreachable_actions:
- .*#recaptcha_enabled?
- .*#valid_recaptcha?
- .*#system_test_entrypoint
- .*#authorize_feature
- .*#authorized_feature?
- .*#authorized_feature!
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ gem 'diffy'
gem 'discord-notifier'
gem 'discordrb', '~> 3.5', require: false
gem 'doorkeeper'
gem 'flipper-active_record', '~> 1.3'
gem 'good_job', '~> 3.14', github: 'komagata/good_job'
gem 'google-cloud-storage', '~> 1.25', require: false
gem 'holiday_jp'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ GEM
ffi (1.17.1)
ffi (1.17.1-arm64-darwin)
ffi (1.17.1-x86_64-linux-gnu)
flipper (1.3.4)
concurrent-ruby (< 2)
flipper-active_record (1.3.4)
activerecord (>= 4.2, < 9)
flipper (~> 1.3.4)
foreman (0.88.1)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
Expand Down Expand Up @@ -676,6 +681,7 @@ DEPENDENCIES
doorkeeper
dotenv-rails
ffi (= 1.17.1)
flipper-active_record (~> 1.3)
foreman
good_job (~> 3.14)!
google-cloud-storage (~> 1.25)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ApplicationController < ActionController::Base
include Authentication
include TestAuthentication if Rails.env.test?
include FeatureToggle
include PolicyHelper
helper_method :staging?
protect_from_forgery with: :exception
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/concerns/feature_toggle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module FeatureToggle
extend ActiveSupport::Concern

included do
helper_method :authorized_feature?
end

def authorized_feature?(feature_key)
Flipper[feature_key].enabled?(current_user)
end

def authorized_feature!(feature_key)
raise ActionController::RoutingError, 'Feature disabled' unless authorized_feature?(feature_key)
end

def authorize_feature(feature_key, actors)
Array(actors).flatten.each do |actor|
Flipper.enable_actor(feature_key, actor)
end
end
end
45 changes: 45 additions & 0 deletions config/initializers/flipper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Rails.application.configure do
## Memoization ensures that only one adapter call is made per feature per request.
## For more info, see https://www.flippercloud.io/docs/optimization#memoization
# config.flipper.memoize = true

## Flipper preloads all features before each request, which is recommended if:
## * you have a limited number of features (< 100?)
## * most of your requests depend on most of your features
## * you have limited gate data combined across all features (< 1k enabled gates, like individual actors, across all features)
##
## For more info, see https://www.flippercloud.io/docs/optimization#preloading
# config.flipper.preload = true

## Warn or raise an error if an unknown feature is checked
## Can be set to `:warn`, `:raise`, or `false`
# config.flipper.strict = Rails.env.development? && :warn

## Show Flipper checks in logs
# config.flipper.log = true

## Reconfigure Flipper to use the Memory adapter and disable Cloud in tests
# config.flipper.test_help = true

## The path that Flipper Cloud will use to sync features
# config.flipper.cloud_path = "_flipper"

## The instrumenter that Flipper will use. Defaults to ActiveSupport::Notifications.
# config.flipper.instrumenter = ActiveSupport::Notifications
end

Flipper.configure do |config|
## Configure other adapters that you want to use here:
## See http://flippercloud.io/docs/adapters
# config.use Flipper::Adapters::ActiveSupportCacheStore, Rails.cache, expires_in: 5.minutes
end

## Register a group that can be used for enabling features.
##
## Flipper.enable_group :my_feature, :admins
##
## See https://www.flippercloud.io/docs/features#enablement-group
#
# Flipper.register(:admins) do |actor|
# actor.respond_to?(:admin?) && actor.admin?
# end
22 changes: 22 additions & 0 deletions db/migrate/20250616101406_create_flipper_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateFlipperTables < ActiveRecord::Migration[6.1]
def 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.text :value
t.timestamps null: false
end
add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 }
end

def down
drop_table :flipper_gates
drop_table :flipper_features
end
end
16 changes: 16 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,22 @@
t.index ["question"], name: "index_faqs_on_question", unique: true
end

create_table "flipper_features", force: :cascade do |t|
t.string "key", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, 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.text "value"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
end

create_table "followings", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_id"
Expand Down
47 changes: 47 additions & 0 deletions test/controllers/concerns/feature_toggle_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'test_helper'

class FeatureToggleTest < ActionDispatch::IntegrationTest
fixtures :users

class ExampleController < ApplicationController
include FeatureToggle

attr_accessor :current_user
end

setup do
@user = users(:kimura)
@controller = ExampleController.new
@controller.current_user = @user
end

test 'authorized_feature?' do
# All features are disabled by default
assert_not @controller.authorized_feature?(:some_feature)

Flipper.enable(:some_feature, @user)
assert @controller.authorized_feature?(:some_feature)
end

test 'authorized_feature!' do
# All features are disabled by default
error = assert_raises(ActionController::RoutingError) do
@controller.authorized_feature!(:some_feature)
end
assert_equal 'Feature disabled', error.message

Flipper.enable(:some_feature, @user)
assert_nil @controller.authorized_feature!(:some_feature)
end

test 'authorize_feature' do
@controller.authorize_feature(:some_feature, [users(:kensyu), User.admins, User.mentor])

assert_not Flipper[:some_feature].enabled?(@controller.current_user)
assert Flipper[:some_feature].enabled?(users(:kensyu))
assert Flipper[:some_feature].enabled?(User.admins.to_a.sample)
assert Flipper[:some_feature].enabled?(User.mentor.to_a.sample)
end
end