create helper for roles and add FF for moderator role (#17737)

* create helper for roles and add FF for moderator role

* address PR feedback
This commit is contained in:
Dwight Scott 2022-05-24 10:46:30 -04:00 committed by GitHub
parent e62684342f
commit c6f852624f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View file

@ -1,5 +1,17 @@
module Admin
module UsersHelper
def role_options(logged_in_user)
options = { "Base Roles" => Constants::Role::BASE_ROLES }
if logged_in_user.super_admin?
special_roles = Constants::Role::SPECIAL_ROLES
if FeatureFlag.enabled?(:moderator_role)
special_roles = special_roles.dup << "Moderator"
end
options["Special Roles"] = special_roles
end
options
end
def format_last_activity_timestamp(timestamp)
return if timestamp.blank?

View file

@ -48,9 +48,7 @@
<%= form_for(@user, url: user_status_admin_user_path(@user), html: { class: "flex flex-col gap-4", id: nil }) do |f| %>
<div class="crayons-field">
<%= f.label :user_status, "Role", class: "crayons-field__label" %>
<% options = { "Base Roles" => Constants::Role::BASE_ROLES } %>
<% options["Special Roles"] = Constants::Role::SPECIAL_ROLES if current_user.super_admin? %>
<%= f.select(:user_status, grouped_options_for_select(options), { include_blank: "Select role" }, class: "crayons-select") %>
<%= f.select(:user_status, grouped_options_for_select(role_options(current_user)), { include_blank: "Select role" }, class: "crayons-select") %>
</div>
<div class="crayons-field">
<%= f.label :note_for_current_role, "Add a note to this action:", class: "crayons-field__label" %>

View file

@ -1,6 +1,33 @@
require "rails_helper"
describe Admin::UsersHelper do
describe "#role_options" do
let(:user) { create(:user) }
it "returns base roles", :aggregate_failures do
user.add_role(:admin)
roles = helper.role_options(user)
expect(roles).to have_key("Base Roles")
expect(roles["Base Roles"]).to eq Constants::Role::BASE_ROLES
end
it "returns special roles", :aggregate_failures do
user.add_role(:super_admin)
roles = helper.role_options(user)
expect(roles).to have_key("Special Roles")
expect(roles["Special Roles"]).to eq Constants::Role::SPECIAL_ROLES
end
it "adds moderator role when feature flag enabled", :aggregate_failures do
user.add_role(:super_admin)
allow(FeatureFlag).to receive(:enabled?).with(:moderator_role).and_return(true)
roles = helper.role_options(user)
expect(roles).to have_key("Special Roles")
expect(roles["Special Roles"]).to include "Moderator"
end
end
describe "#format_last_activity_timestamp" do
it "renders the proper 'Last activity' date for a user that was active today" do
timestamp = Time.zone.today