From 821f299ded0fcbf33562bd49b445a8113371f2ee Mon Sep 17 00:00:00 2001 From: Raul Gracia Date: Fri, 22 Sep 2023 12:34:01 +0100 Subject: [PATCH] Feature Flags (#252) * add flipper feature flags * fix rubocop --- Gemfile | 3 +++ Gemfile.lock | 19 ++++++++++++++++ config/initializers/flipper.rb | 6 +++++ config/routes.rb | 4 ++++ .../20230915092215_create_flipper_tables.rb | 22 +++++++++++++++++++ db/schema.rb | 16 ++++++++++++++ 6 files changed, 70 insertions(+) create mode 100644 config/initializers/flipper.rb create mode 100644 db/migrate/20230915092215_create_flipper_tables.rb diff --git a/Gemfile b/Gemfile index 7fce22cb..2639c0d5 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,9 @@ gem "phonelib" gem "audited", "~> 5.3", ">= 5.3.3" gem "config", "~> 4.2" gem "devise", "~> 4.9" +gem "flipper" +gem "flipper-active_record" +gem "flipper-ui" gem "httparty", "~> 0.21" gem "invisible_captcha" gem "omniauth-azure-activedirectory-v2" diff --git a/Gemfile.lock b/Gemfile.lock index 9651edb9..82e9f026 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,7 @@ GEM bootsnap (1.16.0) msgpack (~> 1.2) brakeman (6.0.1) + brow (0.4.1) builder (3.2.4) bundler-audit (0.9.1) bundler (>= 1.2.0, < 3) @@ -169,6 +170,18 @@ GEM faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) faraday-net_http (3.0.2) + flipper (1.0.0) + brow (~> 0.4.1) + concurrent-ruby (< 2) + flipper-active_record (1.0.0) + activerecord (>= 4.2, < 8) + flipper (~> 1.0.0) + flipper-ui (1.0.0) + erubi (>= 1.0.0, < 2.0.0) + flipper (~> 1.0.0) + rack (>= 1.4, < 4) + rack-protection (>= 1.5.3, <= 4.0.0) + sanitize (< 7) foreman (0.87.2) fugit (1.8.1) et-orbi (~> 1, >= 1.2.7) @@ -381,6 +394,9 @@ GEM rubocop-factory_bot (~> 2.22) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) + sanitize (6.1.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) scenic (1.7.0) activerecord (>= 4.0.0) railties (>= 4.0.0) @@ -463,6 +479,9 @@ DEPENDENCIES dotenv-rails factory_bot_rails faker + flipper + flipper-active_record + flipper-ui foreman govuk-components (= 4.1.1) govuk_design_system_formbuilder diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb new file mode 100644 index 00000000..b75f9716 --- /dev/null +++ b/config/initializers/flipper.rb @@ -0,0 +1,6 @@ +class CanAccessFlipperUI + def self.matches?(request) + current_user = request.env["warden"].user + current_user.present? + end +end diff --git a/config/routes.rb b/config/routes.rb index 25bd141e..8358f046 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,5 +37,9 @@ resources "reports", only: %i[show index] get "/duplicates", to: "applicants#duplicates" get "/audits", to: "audits#index" + + constraints CanAccessFlipperUI do + mount Flipper::UI.app(Flipper) => "/features" + end end end diff --git a/db/migrate/20230915092215_create_flipper_tables.rb b/db/migrate/20230915092215_create_flipper_tables.rb new file mode 100644 index 00000000..1eae1b6d --- /dev/null +++ b/db/migrate/20230915092215_create_flipper_tables.rb @@ -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 + drop_table :flipper_gates + drop_table :flipper_features + end +end diff --git a/db/schema.rb b/db/schema.rb index 2873ecf2..2a356c60 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -108,6 +108,22 @@ t.index ["user_id", "user_type"], name: "user_index" 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 "forms", force: :cascade do |t| t.string "given_name" t.string "middle_name"