Class: Subflag::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Subflag::Generators::InstallGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/subflag/install_generator.rb
Overview
Generator for setting up Subflag in a Rails application
Usage:
rails generate subflag:install # Default: Subflag Cloud
rails generate subflag:install --backend=subflag # Explicit: Subflag Cloud
rails generate subflag:install --backend=active_record # Self-hosted DB
rails generate subflag:install --backend=memory # Testing only
Class Method Summary collapse
Instance Method Summary collapse
- #create_flags_migration ⇒ Object
- #create_initializer ⇒ Object
-
#postgresql? ⇒ Boolean
Helper for templates to detect PostgreSQL adapter.
- #show_instructions ⇒ Object
Class Method Details
.next_migration_number(dirname) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/generators/subflag/install_generator.rb', line 25 def self.next_migration_number(dirname) if defined?(::ActiveRecord::Generators::Base) ::ActiveRecord::Generators::Base.next_migration_number(dirname) else Time.now.utc.strftime("%Y%m%d%H%M%S") end end |
Instance Method Details
#create_flags_migration ⇒ Object
45 46 47 48 49 50 |
# File 'lib/generators/subflag/install_generator.rb', line 45 def create_flags_migration return unless [:backend] == "active_record" migration_template "create_subflag_flags.rb.tt", "db/migrate/create_subflag_flags.rb" end |
#create_initializer ⇒ Object
41 42 43 |
# File 'lib/generators/subflag/install_generator.rb', line 41 def create_initializer template "initializer.rb.tt", "config/initializers/subflag.rb" end |
#postgresql? ⇒ Boolean
Helper for templates to detect PostgreSQL adapter
34 35 36 37 38 39 |
# File 'lib/generators/subflag/install_generator.rb', line 34 def postgresql? return false unless defined?(::ActiveRecord::Base) adapter = ::ActiveRecord::Base.connection_db_config.adapter.to_s rescue nil adapter&.include?("postgresql") || adapter&.include?("postgis") end |
#show_instructions ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/generators/subflag/install_generator.rb', line 52 def show_instructions say "" case [:backend] when "active_record" say "Subflag installed with ActiveRecord backend!", :green say "" say "Next steps:" say "" say "1. Run the migration:" say " $ rails db:migrate" say "" say "2. Create your first flag:" say "" say " Subflag::Rails::Flag.create!(" say " key: 'new-checkout'," say " value: 'true'," say " value_type: 'boolean'" say " )" say "" say "3. Use flags in your code:" say "" say " if subflag_enabled?(:new_checkout)" say " # ..." say " end" say "" say "When you're ready for a dashboard, environments, and user targeting:" say " https://subflag.com", :yellow say "" when "memory" say "Subflag installed with Memory backend!", :green say "" say "Note: Memory backend is for testing only. Flags reset on restart." say "" say "Set flags in your tests or initializer:" say "" say " Subflag::Rails.provider.set(:new_checkout, true)" say " Subflag::Rails.provider.set(:max_projects, 100)" say "" say "Use flags:" say "" say " subflag_enabled?(:new_checkout) # => true" say " subflag_value(:max_projects, default: 3) # => 100" say "" else # subflag (cloud) say "Subflag installed!", :green say "" say "Next steps:" say "" say "1. Add your API key to Rails credentials:" say " $ rails credentials:edit" say "" say " subflag:" say " api_key: sdk-production-your-key-here" say "" say " Or set SUBFLAG_API_KEY environment variable." say "" say "2. Configure user context in config/initializers/subflag.rb" say "" say "3. Use flags in your code:" say "" say " # Controller (auto-scoped to current_user)" say " if subflag_enabled?(:new_checkout)" say " # ..." say " end" say "" say " max = subflag_value(:max_projects, default: 3)" say "" say "Docs: https://docs.subflag.com/rails" say "" end end |