Skip to content

Commit

Permalink
Implement UI::Configuration so users can customize Flipper UI text
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWheeler committed Nov 18, 2017
1 parent e950c88 commit 3ced5fb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 deletions.
13 changes: 13 additions & 0 deletions lib/flipper/ui.rb
Expand Up @@ -12,6 +12,7 @@
require 'flipper/middleware/setup_env'
require 'flipper/middleware/memoizer'
require 'flipper/ui/middleware'
require 'flipper/ui/configuration'

module Flipper
module UI
Expand All @@ -25,6 +26,9 @@ class << self
# set to false, users of the UI cannot create features. All feature
# creation will need to be done through the conigured flipper instance.
attr_accessor :feature_creation_enabled

# Public: Set attributes on this instance to customize UI text
attr_reader :configuration
end

self.feature_creation_enabled = true
Expand All @@ -49,5 +53,14 @@ def self.app(flipper = nil, options = {})
builder.define_singleton_method(:inspect) { klass.inspect } # pretty rake routes output
builder
end

# Public: yields configuration instance for customizing UI text
def self.configure
yield(configuration)
end

def self.configuration
@configuration ||= ::Flipper::UI::Configuration.new
end
end
end
21 changes: 21 additions & 0 deletions lib/flipper/ui/configuration.rb
@@ -0,0 +1,21 @@
require 'flipper/ui/configuration/option'

module Flipper
module UI
class Configuration
attr_reader :actors,
:delete,
:groups,
:percentage_of_actors,
:percentage_of_time

def initialize
@actors = Option.new("Actors", "Enable actors using the form above.")
@groups = Option.new("Groups", "Enable groups using the form above.")
@percentage_of_actors = Option.new("Percentage of Actors", "Percentage of actors functions independently of percentage of time. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.")
@percentage_of_time = Option.new("Percentage of Time", "Percentage of actors functions independently of percentage of time. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.")
@delete = Option.new("Danger Zone", "Deleting a feature removes it from the list of features and disables it for everyone.")
end
end
end
end
12 changes: 12 additions & 0 deletions lib/flipper/ui/configuration/option.rb
@@ -0,0 +1,12 @@
module Flipper
module UI
class Option
attr_accessor :title, :description

def initialize(title, description)
@title = title
@description = description
end
end
end
end
20 changes: 10 additions & 10 deletions lib/flipper/ui/views/feature.erb
Expand Up @@ -37,7 +37,7 @@
<div class="column one-half">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Percentage of Actors</h3>
<h3 class="panel-title"><%= Flipper::UI.configuration.percentage_of_actors.title %></h3>
</div>
<div class="panel-body">
<form action="<%= script_name %>/features/<%= @feature.key %>/percentage_of_actors" method="post">
Expand All @@ -56,14 +56,14 @@
<input type="text" name="value" <% if @feature.percentage_of_actors_value > 0 %>value="<%= @feature.percentage_of_actors_value %>"<% end %> placeholder="custom (ie: 26, 32, etc.)" class="input-mini">
<input type="submit" name="action" value="Enable" class="btn btn-sm">
</form>
<p class="help"><small>Percentage of actors functions independently of percentage of time. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.</small></p>
<p class="help"><small><%= Flipper::UI.configuration.percentage_of_actors.description %></small></p>
</div>
</div>
</div>
<div class="column one-half">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Percentage of Time</h3>
<h3 class="panel-title"><%= Flipper::UI.configuration.percentage_of_time.title %></h3>
</div>
<div class="panel-body">
<form action="<%= script_name %>/features/<%= @feature.key %>/percentage_of_time" method="post">
Expand All @@ -83,7 +83,7 @@
<input type="submit" name="action" value="Enable" class="btn btn-sm">
</form>

<p class="help"><small>Percentage of time functions independently of percentage of actors. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.</small></p>
<p class="help"><small><%= Flipper::UI.configuration.actors.description %></small></p>
</div>
</div>
</div>
Expand All @@ -110,15 +110,15 @@
<input type="submit" value="Add Group" class="btn btn-sm">
</form>
<% end %>
<h3 class="panel-title">Groups</h3>
<h3 class="panel-title"><%= Flipper::UI.configuration.groups.title %></h3>
</div>
<% if @feature.groups_value.empty? %>
<div class="blankslate">
<span class="mega-octicon octicon-organization"></span>
<span class="mega-octicon octicon-squirrel"></span>
<span class="mega-octicon octicon-zap"></span>
<h3>No Enabled Groups</h3>
<p>Enable groups using the form above.</p>
<p><%= Flipper::UI.configuration.groups.description %></p>
</div>
<% else %>
<ul class="list-group">
Expand Down Expand Up @@ -154,15 +154,15 @@
<input type="text" name="value" placeholder="ie: User:6" class="input-mini">
<input type="submit" value="Add Actor" class="btn btn-sm">
</form>
<h3 class="panel-title">Actors</h3>
<h3 class="panel-title"><%= Flipper::UI.configuration.actors.title %></h3>
</div>
<% if @feature.actors_value.empty? %>
<div class="blankslate">
<span class="mega-octicon octicon-person"></span>
<span class="mega-octicon octicon-squirrel"></span>
<span class="mega-octicon octicon-zap"></span>
<h3>No Enabled Actors</h3>
<p>Enable actors using the form above.</p>
<p><%= Flipper::UI.configuration.actors.description %></p>
</div>
<% else %>
<ul class="list-group">
Expand Down Expand Up @@ -194,11 +194,11 @@

<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Danger Zone</h3>
<h3 class="panel-title"><%= Flipper::UI.configuration.delete.title %></h3>
</div>
<div class="panel-body">
<p>
Deleting a feature removes it from the list of features and disables it for everyone.
<%= Flipper::UI.configuration.delete.description %>
</p>

<form action="<%= script_name %>/features/<%= @feature.key %>" method="post" onsubmit="return confirm('Are you sure you want to remove <%= @feature.key %> from the list of features and disable it for everyone?')">
Expand Down
45 changes: 45 additions & 0 deletions spec/flipper/ui/configuration_spec.rb
@@ -0,0 +1,45 @@
require 'helper'

RSpec.describe Flipper::UI::Configuration do
let(:configuration) { described_class.new }

describe "#actors" do
it "has default text" do
expect(configuration.actors.title).to eq("Actors")
expect(configuration.actors.description).to eq("Enable actors using the form above.")
end

it "can be updated" do
configuration.actors.title = "Actors Section"
expect(configuration.actors.title).to eq("Actors Section")
end
end

describe "#groups" do
it "has default text" do
expect(configuration.groups.title).to eq("Groups")
expect(configuration.groups.description).to eq("Enable groups using the form above.")
end
end

describe "#percentage_of_actors" do
it "has default text" do
expect(configuration.percentage_of_actors.title).to eq("Percentage of Actors")
expect(configuration.percentage_of_actors.description).to eq("Percentage of actors functions independently of percentage of time. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.")
end
end

describe "#percentage_of_time" do
it "has default text" do
expect(configuration.percentage_of_time.title).to eq("Percentage of Time")
expect(configuration.percentage_of_time.description).to eq("Percentage of actors functions independently of percentage of time. If you enable 50% of Actors and 25% of Time then the feature will always be enabled for 50% of users and occasionally enabled 25% of the time for everyone.")
end
end

describe "#delete" do
it "has default text" do
expect(configuration.delete.title).to eq("Danger Zone")
expect(configuration.delete.description).to eq("Deleting a feature removes it from the list of features and disables it for everyone.")
end
end
end
8 changes: 8 additions & 0 deletions spec/flipper/ui_spec.rb
Expand Up @@ -146,4 +146,12 @@
described_class.feature_creation_enabled = @original_feature_creation_enabled
end
end

describe 'configure' do
it 'yields configuration instance' do
described_class.configure do |config|
expect(config).to be_instance_of(Flipper::UI::Configuration)
end
end
end
end

0 comments on commit 3ced5fb

Please sign in to comment.