From 9d6c991158c51d7bbc7498936f442c80d7769ccd Mon Sep 17 00:00:00 2001 From: Raul Gracia Date: Fri, 15 Sep 2023 12:46:41 +0100 Subject: [PATCH 1/2] add flipper feature flags --- Gemfile | 3 +++ Gemfile.lock | 19 ++++++++++++++++ config/initializers/flipper.rb | 6 +++++ config/routes.rb | 4 ++++ .../20230915092215_create_flipper_tables.rb | 22 +++++++++++++++++++ db/schema.rb | 18 ++++++++++++++- 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 config/initializers/flipper.rb create mode 100644 db/migrate/20230915092215_create_flipper_tables.rb diff --git a/Gemfile b/Gemfile index ccbd5621..525c5936 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,9 @@ gem "httparty", "~> 0.21" gem "invisible_captcha" gem "omniauth-azure-activedirectory-v2" gem "sentry-rails", "~> 5.11" +gem 'flipper' +gem 'flipper-active_record' +gem 'flipper-ui' group :test do gem "shoulda-matchers", "~> 5.0" diff --git a/Gemfile.lock b/Gemfile.lock index 58fe97b0..69cf74ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,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) @@ -167,6 +168,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) @@ -379,6 +392,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) @@ -461,6 +477,9 @@ DEPENDENCIES dotenv-rails factory_bot_rails faker + flipper + flipper-active_record + flipper-ui foreman govuk-components (= 4.1.0) govuk_design_system_formbuilder diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb new file mode 100644 index 00000000..30d1efeb --- /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..c2e2d446 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..b07fd689 --- /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, [: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 dc0b3ea8..bf5881d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_09_06_030846) do +ActiveRecord::Schema[7.0].define(version: 2023_09_15_092215) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "plpgsql" @@ -107,6 +107,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" From d5a6a9f4e673c0d566a51ad70909127937cd0d29 Mon Sep 17 00:00:00 2001 From: Raul Gracia Date: Wed, 20 Sep 2023 02:25:37 +0100 Subject: [PATCH 2/2] fix rubocop --- Gemfile | 6 +++--- config/initializers/flipper.rb | 2 +- config/routes.rb | 2 +- db/migrate/20230915092215_create_flipper_tables.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 525c5936..f4d5c9ac 100644 --- a/Gemfile +++ b/Gemfile @@ -35,13 +35,13 @@ 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" gem "sentry-rails", "~> 5.11" -gem 'flipper' -gem 'flipper-active_record' -gem 'flipper-ui' group :test do gem "shoulda-matchers", "~> 5.0" diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb index 30d1efeb..b75f9716 100644 --- a/config/initializers/flipper.rb +++ b/config/initializers/flipper.rb @@ -1,6 +1,6 @@ class CanAccessFlipperUI def self.matches?(request) - current_user = request.env['warden'].user + current_user = request.env["warden"].user current_user.present? end end diff --git a/config/routes.rb b/config/routes.rb index c2e2d446..8358f046 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,7 +39,7 @@ get "/audits", to: "audits#index" constraints CanAccessFlipperUI do - mount Flipper::UI.app(Flipper) => '/features' + 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 index b07fd689..1eae1b6d 100644 --- a/db/migrate/20230915092215_create_flipper_tables.rb +++ b/db/migrate/20230915092215_create_flipper_tables.rb @@ -12,7 +12,7 @@ def self.up t.string :value t.timestamps null: false end - add_index :flipper_gates, [:feature_key, :key, :value], unique: true + add_index :flipper_gates, %i[feature_key key value], unique: true end def self.down