Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature Flag controls #5273

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@use "pages/case_contacts";
@use "pages/court_dates";
@use "pages/emancipation";
@use "pages/feature_flags";
@use "pages/login";
@use "pages/password";
@use "pages/patch_notes";
Expand Down
9 changes: 9 additions & 0 deletions app/assets/stylesheets/pages/feature_flags.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#feature-flags {
padding-top: 1em;
h1 {
padding-bottom: 1em;
}
.flag-name {
padding-right: 2em;
}
}
10 changes: 10 additions & 0 deletions app/controllers/all_casa_admins/feature_flags_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AllCasaAdmins::FeatureFlagsController < AllCasaAdminsController
def index
@feature_flags = FeatureFlag.all.order(name: "asc")
end

def update
@feature_flag = FeatureFlag.find(params[:id])
@feature_flag.update(enabled: !@feature_flag.enabled)
end
end
1 change: 1 addition & 0 deletions app/javascript/all_casa_admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('./src/all_casa_admin/tables')
require('./src/all_casa_admin/patch_notes')
require('./src/all_casa_admin/feature_flags')
require('./src/session_timeout_poller.js')
require('./src/display_app_metric.js')
30 changes: 30 additions & 0 deletions app/javascript/src/all_casa_admin/feature_flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$(function () {
const checkboxes = $('.form-check-input.toggle-switch')

checkboxes.on('change', function (e) {
e.preventDefault()
const featureFlagId = $(this).data('feature-flag-id')
const uniqueCheckbox = $('#feature-checkbox-' + featureFlagId)
const csrfToken = $('meta[name=csrf-token]').attr('content')
const isChecked = this.checked
toggleSwitch(uniqueCheckbox, csrfToken, featureFlagId, isChecked)
})

function toggleSwitch (uniqueCheckbox, csrfToken, featureFlagId, checked) {
$.ajax({
url: `/all_casa_admins/feature_flags/${featureFlagId}`,
method: 'PATCH',
contentType: 'application/json',
headers: {
'X-CSRF-Token': csrfToken
},
success: function () {
if (checked) {
uniqueCheckbox.prop('checked', true)
} else {
uniqueCheckbox.prop('checked', false)
}
}
})
}
})
1 change: 1 addition & 0 deletions app/models/feature_flag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class FeatureFlag < ApplicationRecord
validates :name, presence: true, uniqueness: true
end

# == Schema Information
Expand Down
18 changes: 18 additions & 0 deletions app/views/all_casa_admins/feature_flags/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div id=feature-flags>
<h1>Feature Flags</h1>
<table>
<tbody>
<% @feature_flags.each do |feature_flag| %>
<tr class="flag-rows">
<td class="flag-name"><%= feature_flag.name %></td>
<td>
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input toggle-switch" data-feature-flag-id="<%= feature_flag.id %>" id="feature-checkbox-<%= feature_flag.id %>" <% if feature_flag.enabled? %> checked <% end %>>
<input type="hidden" name="feature_flag[enabled]" value="0" id="hidden_submit">
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
6 changes: 6 additions & 0 deletions app/views/layouts/_all_casa_admin_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
Edit Profile
<% end %>
</li>
<li class="<%= active_class(all_casa_admins_feature_flags_path) %> nav-item">
<%= link_to all_casa_admins_feature_flags_path do %>
<i class="lni lni-pencil-alt mr-10"></i>
Feature Flags
<% end %>
</li>
<li class="nav-item">
<%= link_to destroy_all_casa_admin_session_path, id:"all-casa-log-out" do %>
<i class="lni lni-exit mr-10"></i>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
end

resources :patch_notes, only: %i[create destroy index update]

resources :feature_flags, only: %i[index update]
end

resources :all_casa_admins, only: [:new, :create] do
Expand Down
2 changes: 0 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema[7.0].define(version: 2023_09_03_182657) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
2 changes: 1 addition & 1 deletion public/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -3859,4 +3859,4 @@ h6,
font-weight: 800; }

.fw-900 {
font-weight: 900; }
font-weight: 900; }
2 changes: 1 addition & 1 deletion spec/models/feature_flag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
expect {
create(:feature_flag, name: duplicate_feature_flag_name)
create(:feature_flag, name: duplicate_feature_flag_name)
}.to raise_error(ActiveRecord::RecordNotUnique)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
26 changes: 26 additions & 0 deletions spec/requests/all_casa_admins/feature_flags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe "/all_casa_admins/feature_flags", type: :request do
let(:all_casa_admin) { build(:all_casa_admin) }
let(:feature_flag) { create(:feature_flag) }

before(:each) { sign_in all_casa_admin }

describe "GET /index" do
it "gets all the feature flags" do
get all_casa_admins_feature_flags_path

expect(response).to render_template(:index)
expect(response).to be_successful
end
end

describe "PATCH /update" do
it "updates enable to false on toggle" do
patch "/all_casa_admins/feature_flags/#{feature_flag.id}"

feature_flag.reload
expect(feature_flag.enabled).to eq(false)
end
end
end
20 changes: 20 additions & 0 deletions spec/views/all_casa_admins/feature_flags/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rails_helper"

RSpec.describe "patch_notes/index", type: :view do
let(:all_casa_admin) { build(:all_casa_admin) }

before(:each) { sign_in all_casa_admin }

describe "the feature flag list" do
it "displays the feature flags with toggles" do
feature_flag_1 = create(:feature_flag, name: "Test feature one")
feature_flag_2 = create(:feature_flag, name: "Test feature two")
assign(:feature_flags, [feature_flag_1, feature_flag_2])

render template: "all_casa_admins/feature_flags/index"

expect(rendered).to include("Test feature one")
expect(rendered).to include("Test feature two")
end
end
end