Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into expressions-schema
Browse files Browse the repository at this point in the history
* origin/main:
  Add changelog note for human readable names
  Use human action name if present
  Make sure actor_names_source can be updated
  Add actor_names_source to configuration
  • Loading branch information
bkeepers committed Nov 10, 2023
2 parents 5470e59 + b0d56f2 commit afb896a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

### Additions/Changes

* Human readable actor names in flipper-ui (https://github.com/flippercloud/flipper/pull/737).
* Expressions are now available and considered "alpha". They are not yet documented, but you can see examples in [examples/expressions.rb](examples/expressions.rb). (https://github.com/flippercloud/flipper/pull/692)
* Allow head requests to api (https://github.com/flippercloud/flipper/pull/759)

Expand Down
7 changes: 7 additions & 0 deletions examples/ui/basic.ru
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Flipper::UI.configure do |config|
"a/b" => "Why would someone use a slash? I don't know but someone did. Let's make this really long so they regret using slashes. Please don't use slashes.",
}
end

config.actor_names_source = lambda do |_keys|
{
'1' => 'John',
'6' => 'Brandon',
}
end
end

# You can uncomment these to get some default data:
Expand Down
1 change: 1 addition & 0 deletions lib/flipper/ui/actions/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def get
@feature = Decorators::Feature.new(flipper_feature)
descriptions = Flipper::UI.configuration.descriptions_source.call([flipper_feature.key])
@feature.description = descriptions[@feature.key]
@feature.actor_names = Flipper::UI.configuration.actor_names_source.call(@feature.actors_value)
@page_title = "#{@feature.key} // Features"
@percentages = [0, 1, 5, 10, 25, 50, 100]

Expand Down
7 changes: 7 additions & 0 deletions lib/flipper/ui/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Configuration
# page, and optionally the `features` pages. Defaults to empty block.
attr_accessor :descriptions_source

# Public: If you set this, Flipper::UI will fetch actor names
# from your external source. Descriptions for `actors` will be shown on `feature`
# page. Defaults to empty block.
attr_accessor :actor_names_source

# Public: Should feature descriptions be show on the `features` list page.
# Default false. Only works when using descriptions.
attr_accessor :show_feature_description_in_list
Expand All @@ -70,6 +75,7 @@ class Configuration
).freeze

DEFAULT_DESCRIPTIONS_SOURCE = ->(_keys) { {} }
DEFAULT_ACTOR_NAMES_SOURCE = ->(_keys) { {} }

def initialize
@delete = Option.new("Danger Zone", "Deleting a feature removes it from the list of features and disables it for everyone.")
Expand All @@ -81,6 +87,7 @@ def initialize
@cloud_recommendation = true
@add_actor_placeholder = "a flipper id"
@descriptions_source = DEFAULT_DESCRIPTIONS_SOURCE
@actor_names_source = DEFAULT_ACTOR_NAMES_SOURCE
@show_feature_description_in_list = false
@actors_separator = ','
@confirm_fully_enable = false
Expand Down
4 changes: 4 additions & 0 deletions lib/flipper/ui/decorators/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Feature < SimpleDelegator
# configured for Flipper::UI.
attr_accessor :description

# Internal: Used to preload actor names if actor_names_source is
# configured for Flipper::UI.
attr_accessor :actor_names

# Public: Returns name titleized.
def pretty_name
@pretty_name ||= Util.titleize(name)
Expand Down
8 changes: 7 additions & 1 deletion lib/flipper/ui/views/feature.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@
<div class="card-body bg-lightest border-bottom py-3">
<div class="row align-items-center">
<div class="col col-mr-auto pl-md-5">
<h6 class="m-0"><%= item %></h6>
<h6 class="m-0">
<% if Flipper::UI::Util.present?(@feature.actor_names[item]) %>
<%= "#{@feature.actor_names[item]} (#{item})" %>
<% else %>
<%= item %>
<% end %>
</h6>
</div>
<div class="col col-auto">
<% unless Flipper::UI.configuration.read_only %>
Expand Down
23 changes: 23 additions & 0 deletions spec/flipper/ui/actions/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@
expect(last_response.body).to include('Enabled for 0% of actors')
expect(last_response.body).to include('Most in-depth search')
end

context 'custom actor names' do
before do
actor = Flipper::Actor.new('some_actor_name')
flipper['search'].enable_actor(actor)

Flipper::UI.configure do |config|
config.actor_names_source = lambda { |_keys|
{
"some_actor_name" => "Some Actor Name",
"some_other_actor_name" => "Some Other Actor Name",
}
}
end

get '/features/search'
end

it 'renders template with custom actor names' do
expect(last_response.body).to include('Some Actor Name (some_actor_name)')
expect(last_response.body).not_to include('Some Other Actor Name')
end
end
end

describe 'GET /features/:feature with _features in feature name' do
Expand Down
20 changes: 20 additions & 0 deletions spec/flipper/ui/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@
end
end

describe "#actor_names_source" do
it "has default value" do
expect(configuration.actor_names_source.call(%w[foo bar])).to eq({})
end

context "actor names source is provided" do
it "can be updated" do
configuration.actor_names_source = lambda do |_keys|
YAML.load_file(FlipperRoot.join('spec/support/actor_names.yml'))
end
keys = %w[actor_1 foo]
result = configuration.actor_names_source.call(keys)
expected = {
"actor_name_1" => "Actor #1",
}
expect(result).to eq(expected)
end
end
end

describe "#confirm_fully_enable" do
it "has default value" do
expect(configuration.confirm_fully_enable).to eq(false)
Expand Down
1 change: 1 addition & 0 deletions spec/support/actor_names.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
actor_name_1: 'Actor #1'

0 comments on commit afb896a

Please sign in to comment.