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

Generate a default initializer #815

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions lib/generators/flipper/setup_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ module Flipper
module Generators
class SetupGenerator < ::Rails::Generators::Base
desc 'Peform any necessary steps to install Flipper'
source_paths << File.expand_path('templates', __dir__)

class_option :token, type: :string, default: nil, aliases: '-t',
desc: "Your personal environment token for Flipper Cloud"

def generate_initializer
template 'initializer.rb', 'config/initializers/flipper.rb'
end

def generate_active_record
invoke 'flipper:active_record' if defined?(Flipper::Adapters::ActiveRecord)
end
Expand Down
45 changes: 45 additions & 0 deletions lib/generators/flipper/templates/initializer.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 adaper 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
5 changes: 5 additions & 0 deletions test_rails/generators/flipper/setup_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class SetupGeneratorTest < Rails::Generators::TestCase
end
end

test "generates an initializer" do
run_generator
assert_file 'config/initializers/flipper.rb', /Flipper\.configure/
end

test "does not invoke flipper:active_record generator if ActiveRecord adapter not defined" do
# Ensure adapter not defined
Flipper::Adapters.send(:remove_const, :ActiveRecord) rescue nil
Expand Down