-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature flags --------- Co-authored-by: Jano Suchal <[email protected]>
- Loading branch information
1 parent
0839fd7
commit 42dde8d
Showing
11 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
app/components/admin/feature_flags/feature_flags_list_row_component.html.erb
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,21 @@ | ||
<div class="self-stretch p-4 border-b border-gray-200 items-center gap-4 inline-flex"> | ||
<div class="grow shrink basis-0 gap-1"> | ||
<div class="text-gray-900 text-lg font-medium leading-loose w-fit"> | ||
<div class="grow shrink basis-0 flex-col justify-start items-start gap-1 inline-flex"> | ||
<div class="text-center text-gray-900 text-lg font-medium leading-loose"> | ||
<%= t "feature_flags.#{@feature_flag}.name"%> | ||
</div> | ||
<div class="text-center text-gray-500 text-base font-normal leading-normal"> | ||
<%= t "feature_flags.#{@feature_flag}.description"%> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<%= form_with model: Current.tenant, url: admin_tenant_feature_flag_path(Current.tenant, @feature_flag), title: "Toggle feature state", method: :patch do |form| %> | ||
<%= form.hidden_field :enabled, value: !@enabled %> | ||
<%= form.button name: @feature_flag, class: "#{@enabled ? "bg-indigo-600" : "bg-gray-200"} relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2", role: :switch, aria: { checked: @enabled } do %> | ||
<span class="sr-only">Use setting</span> | ||
<span aria-hidden="true" class="<%= @enabled ? "translate-x-5" : "translate-x-0" %> pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span> | ||
<% end %> | ||
<% end %> | ||
</div> |
6 changes: 6 additions & 0 deletions
6
app/components/admin/feature_flags/feature_flags_list_row_component.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,6 @@ | ||
class Admin::FeatureFlags::FeatureFlagsListRowComponent < ViewComponent::Base | ||
def initialize(flag, enabled_features) | ||
@feature_flag = flag | ||
@enabled = enabled_features.include?(flag) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Admin::FeatureFlagsController < ApplicationController | ||
before_action :set_tenant | ||
|
||
def index | ||
authorize([:admin, :feature_flag]) | ||
if params.include?(:labs) | ||
@feature_flags = @tenant.list_all_features | ||
else | ||
@feature_flags = @tenant.list_available_features | ||
end | ||
@enabled_features = @tenant.feature_flags | ||
end | ||
|
||
def update | ||
authorize([:admin, :feature_flag]) | ||
if feature_flags_params[:enabled] == "true" | ||
@tenant.feature_flags << params[:id] | ||
else | ||
@tenant.feature_flags.delete(params[:id]) | ||
end | ||
@tenant.save! | ||
redirect_to admin_tenant_feature_flags_path | ||
end | ||
|
||
private | ||
|
||
def set_tenant | ||
@tenant = policy_scope([:admin, :feature_flag]).find(params[:tenant_id]) | ||
end | ||
|
||
def feature_flags_params | ||
params.require(:tenant).permit(:enabled) | ||
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class Admin::FeatureFlagPolicy < ApplicationPolicy | ||
attr_reader :user, :tenant | ||
|
||
def initialize(user, tenant) | ||
@user = user | ||
@tenant = tenant | ||
end | ||
|
||
class Scope < Scope | ||
def resolve | ||
Tenant.where(id: @user.tenant) | ||
end | ||
end | ||
|
||
def index? | ||
@user.admin? | ||
end | ||
|
||
def update? | ||
@user.admin? | ||
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,12 @@ | ||
<div class="w-full p-4 flex-col justify-start items-start gap-4 inline-flex"> | ||
<div class="self-stretch bg-white rounded-md border border-gray-200 flex-col justify-start items-start flex"> | ||
<div class="self-stretch p-6 border-b border-gray-200 justify-start items-center gap-4 inline-flex"> | ||
<div class="grow shrink basis-0 text-gray-900 text-xl font-semibold leading-[35px]">Aktivácia rozšírení</div> | ||
</div> | ||
<% @feature_flags.each do |flag| %> | ||
<div class="self-stretch flex-col justify-start items-start flex"> | ||
<%= render Admin::FeatureFlags::FeatureFlagsListRowComponent.new(flag.to_s, @enabled_features) %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
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,19 @@ | ||
require "application_system_test_case" | ||
|
||
class FeatureFlagsManagementTest < ApplicationSystemTestCase | ||
setup do | ||
sign_in_as(:admin) | ||
visit root_path | ||
click_link "Nastavenia" | ||
click_link "Funkcie" | ||
end | ||
|
||
test "admin can enable and disable a feature" do | ||
available_features = users(:admin).tenant.list_available_features | ||
enabled = users(:admin).tenant.feature_enabled?(available_features[0]) | ||
click_button available_features[0] | ||
assert_button available_features[0] | ||
users(:admin).tenant.reload | ||
assert_not_equal enabled, users(:admin).tenant.feature_enabled?(available_features[0]) | ||
end | ||
end |