Skip to content

Commit

Permalink
feature flag routing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlences committed Dec 1, 2024
1 parent d8e9945 commit 815bbca
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</div>
</div>
</div>
<%= form_with model: Current.tenant, url: admin_tenant_feature_flags_path(Current.tenant), title: "Toggle feature state", method: :patch do |form| %>
<%= form.hidden_field :feature_flags, value: @features_changed.join(",") %>
<%= 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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ class Admin::FeatureFlags::FeatureFlagsListRowComponent < ViewComponent::Base
def initialize(flag, enabled_features)
@feature_flag = flag
@enabled = enabled_features.include?(flag)
@features_changed = enabled_features.include?(flag) ? enabled_features - [flag] : enabled_features + [flag]
end
end
4 changes: 2 additions & 2 deletions app/controllers/admin/feature_flags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index

def update
authorize([:admin, :feature_flag])
@tenant.feature_flags = feature_flags_params["feature_flags"].split(",")
@tenant.feature_flags = feature_flags_params[:enabled] == "true" ? @tenant.feature_flags.union([params[:id]]) : @tenant.feature_flags - [params[:id]]
@tenant.save!
redirect_to admin_tenant_feature_flags_path
end
Expand All @@ -21,6 +21,6 @@ def set_tenant
end

def feature_flags_params
params.require(:tenant).permit(:feature_flags)
params.require(:tenant).permit(:enabled)
end
end
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
end

resources :users

get "/feature_flags/", to: "feature_flags#index"
patch "/feature_flags/", to: "feature_flags#update"
resources :feature_flags, only: [:index, :update]

resources :boxes, only: :index
namespace :boxes do
Expand Down

0 comments on commit 815bbca

Please sign in to comment.