Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12297 from dmarcoux/opt-out-of-feature-toggles
Allow users to manage beta features
- Loading branch information
Showing
21 changed files
with
329 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/api/app/controllers/webui/users/beta_features_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Webui::Users::BetaFeaturesController < Webui::WebuiController | ||
after_action :verify_policy_scoped | ||
|
||
def index | ||
@disabled_beta_features = policy_scope(DisabledBetaFeature).pluck(:name) | ||
@user = User.session! | ||
end | ||
|
||
def update | ||
feature_name = feature_params.keys.first | ||
feature_action = feature_params.values.first | ||
|
||
if feature_action == 'disable' | ||
begin | ||
policy_scope(DisabledBetaFeature).create!(name: feature_name) | ||
flash[:success] = "You disabled the beta feature '#{feature_name.humanize}'." | ||
rescue ActiveRecord::RecordInvalid | ||
flash[:error] = "You already disabled the beta feature '#{feature_name.humanize}'." | ||
end | ||
else | ||
disabled_beta_feature = policy_scope(DisabledBetaFeature).find_by(name: feature_name) | ||
if disabled_beta_feature && disabled_beta_feature.destroy | ||
flash[:success] = "You enabled the beta feature '#{feature_name.humanize}'." | ||
else | ||
flash[:error] = "You already enabled the beta feature '#{feature_name.humanize}'." | ||
end | ||
end | ||
|
||
redirect_to my_beta_features_path | ||
end | ||
|
||
private | ||
|
||
def feature_params | ||
params.require(:feature).permit(ENABLED_FEATURE_TOGGLES.pluck(:name)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class DisabledBetaFeature < ApplicationRecord | ||
validates :name, presence: true | ||
validates :user_id, uniqueness: { scope: :name } | ||
|
||
belongs_to :user | ||
# rubocop:disable Rails/InverseOf | ||
# This is an internal model from the flipper-active_record gem, so we cannot set inverse_of | ||
belongs_to :feature, foreign_key: :name, class_name: 'Flipper::Adapters::ActiveRecord::Feature', primary_key: :key | ||
# rubocop:enable Rails/InverseOf | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: disabled_beta_features | ||
# | ||
# id :integer not null, primary key | ||
# name :string(255) not null, indexed => [user_id] | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# user_id :integer indexed => [name] | ||
# | ||
# Indexes | ||
# | ||
# index_disabled_beta_features_on_user_id_and_name (user_id,name) UNIQUE | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class DisabledBetaFeaturePolicy < ApplicationPolicy | ||
class Scope < Scope | ||
def initialize(user, scope) | ||
raise Pundit::NotAuthorizedError, reason: ApplicationPolicy::ANONYMOUS_USER if user.nil? || user.is_nobody? | ||
|
||
super(user, scope) | ||
end | ||
|
||
def resolve | ||
scope.where(user: user) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/api/app/views/webui/user/index_actions/_manage_beta_features.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
%li.nav-item | ||
= link_to(my_beta_features_path, class: 'nav-link', title: 'Manage Beta Features') do | ||
%i.fas.fa-lg.mr-2.fa-flask | ||
%span.nav-item-name Manage Beta Features |
4 changes: 4 additions & 0 deletions
4
src/api/app/views/webui/users/beta_features/_breadcrumb_items.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
%li.breadcrumb-item | ||
= link_to('Your Profile', user_path(@user)) | ||
%li.breadcrumb-item.active{ 'aria-current' => 'page' } | ||
Manage Beta Features |
43 changes: 43 additions & 0 deletions
43
src/api/app/views/webui/users/beta_features/index.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
- @pagetitle = 'Manage Beta Features' | ||
|
||
.card | ||
.card-body | ||
%h2= @pagetitle | ||
|
||
.my-4 | ||
- if @user.in_beta? | ||
%p | ||
Thank you for joining the beta program! Give us | ||
= link_to('feedback', 'https://openbuildservice.org/2018/10/04/the-beta-program/#how-to-provide-feedback') | ||
on the features before they go live. | ||
- else | ||
%p | ||
Join the beta program to try the latest features we develop and give us feedback on them before they go live. | ||
Read our | ||
= link_to('blog post', 'https://openbuildservice.org/2018/10/04/the-beta-program/') | ||
on the beta program for more details. | ||
|
||
= form_with(url: user_path(@user), method: :patch) do |_form| | ||
.custom-control.custom-switch | ||
= hidden_field_tag('user[in_beta]', false, id: nil) | ||
= check_box_tag('user[in_beta]', !@user.in_beta, @user.in_beta, class: 'custom-control-input', onChange: 'this.form.submit()') | ||
= label_tag('user[in_beta]', 'Beta program', class: 'custom-control-label') | ||
|
||
- if @user.in_beta? | ||
%hr | ||
|
||
%p By default, all beta features are enabled as soon as you join the beta program. | ||
%p | ||
You can disable a specific beta feature if you want. | ||
Enabling back a beta feature is always possible if you change your mind later. | ||
|
||
- ENABLED_FEATURE_TOGGLES.each do |feature_toggle| | ||
- feature_toggle_name, feature_toggle_description = feature_toggle.values_at(:name, :description).map(&:to_s) | ||
.mb-2 | ||
%h3= feature_toggle_name.humanize | ||
= form_with(url: my_beta_feature_path, method: :put) do |_form| | ||
.custom-control.custom-switch | ||
= hidden_field_tag("feature[#{feature_toggle_name}]", 'disable', id: nil) | ||
= check_box_tag("feature[#{feature_toggle_name}]", 'enable', @disabled_beta_features.exclude?(feature_toggle_name), | ||
class: 'custom-control-input', onChange: 'this.form.submit()') | ||
= label_tag("feature[#{feature_toggle_name}]", feature_toggle_description, class: 'custom-control-label') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/api/db/migrate/20220309122242_create_disabled_beta_features.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateDisabledBetaFeatures < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table(:disabled_beta_features, id: :integer) do |t| | ||
t.string :name, null: false | ||
t.references :user, type: :integer, index: false | ||
|
||
t.timestamps | ||
|
||
t.index [:user_id, :name], unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace :flipper do | ||
desc 'Enable feature toggles from ENABLED_FEATURE_TOGGLES for their group' | ||
task enable_features_for_group: :environment do | ||
ENABLED_FEATURE_TOGGLES.each do |feature_toggle| | ||
feature_toggle_name = feature_toggle[:name] | ||
# Enable the feature toggle for group with the same name | ||
Flipper.enable(feature_toggle_name, feature_toggle_name) | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
src/api/spec/controllers/webui/users/beta_features_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Webui::Users::BetaFeaturesController do | ||
describe 'when the user is anonymous' do | ||
before do | ||
get :index | ||
end | ||
|
||
it { expect(response).to have_http_status(:found) } | ||
it { expect(response).to redirect_to(new_session_path) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :disabled_beta_feature do | ||
name { 'something' } # It needs to be enabled: Flipper[name].enable | ||
user | ||
end | ||
end |
Oops, something went wrong.