Skip to content

Commit

Permalink
Merge branch 'release/2.5.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Abbett committed Jun 14, 2022
2 parents e29a4b2 + bb8a143 commit 75f4e69
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,29 @@ my_tour:
* `action` is one of the Shepherd tour method names, i.e. `cancel`, `next`, or `complete`
* `classes` are the CSS class names you want applied to the button
### Flipper integration
If you have [Flipper](https://github.com/jnunemaker/flipper) installed as a dependency in your project you will be able to enable or disable tours based on a flipper using the `flipper_key` option. This will automatically enable a tour when this flipper is active and disable it when it's inactive.
```yml
walkthrough:
flipper_key: "name_of_flipper"
steps:
1:
text: "This walkthrough will show you how to..."
```

If you would like to disable a tour when a flipper is active you may couple the `flipper_key` option with the `flipper_activation` option. `flipper_activation` supports "enabled" or "disabled" as options. If you enter something other than "enabled" or "disabled" it will use the default, which is "enabled".

```yml
walkthrough:
flipper_key: "name_of_flipper"
flipper_activation: "disabled"
steps:
1:
text: "This walkthrough will show you how to..."
```
### Testing your tours
Abraham loads tour definitions once when you start your server. Restart your server to see tour changes.
Expand Down
17 changes: 12 additions & 5 deletions app/helpers/abraham_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module AbrahamHelper
include FlipperHelper

def abraham_tour
# Do we have tours for this controller/action in the user's locale?
tours = Rails.configuration.abraham.tours["#{controller_path}.#{action_name}.#{I18n.locale}"]
Expand All @@ -21,11 +23,16 @@ def abraham_tour
tour_html = ''

tour_keys.each do |key|
tour_html += render(partial: "application/abraham",
locals: { tour_name: key,
tour_completed: tour_keys_completed.include?(key),
trigger: tours[key]["trigger"],
steps: tours[key]["steps"] })
flipper_key = tours[key]["flipper_key"]
flipper_activation = tours[key]["flipper_activation"]

if should_add_tour(flipper_key, flipper_activation)
tour_html += render(partial: "application/abraham",
locals: { tour_name: key,
tour_completed: tour_keys_completed.include?(key),
trigger: tours[key]["trigger"],
steps: tours[key]["steps"] })
end
end

tour_html.html_safe
Expand Down
26 changes: 26 additions & 0 deletions app/helpers/flipper_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module FlipperHelper
def should_add_tour(flipper_key, flipper_activation)
return true if !flipper_defined?

case process_activation_option(flipper_activation)
when "enabled"
return (flipper_key && Flipper.enabled?(flipper_key.to_sym)) || flipper_key.nil?
when "disabled"
return (flipper_key && !Flipper.enabled?(flipper_key.to_sym)) || flipper_key.nil?
else
return false
end
end

private
def flipper_defined?
Object.const_defined?("Flipper")
end

def process_activation_option(flipper_activation)
return "disabled" if flipper_activation == "disabled"
"enabled"
end
end
2 changes: 1 addition & 1 deletion lib/abraham/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Abraham
VERSION = "2.5.0"
VERSION = "2.5.1"
end
33 changes: 33 additions & 0 deletions test/dummy/test/helpers/flipper_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require "test_helper"

class FlipperHelperTest < ActionView::TestCase
test "Add tour whenever all options nil" do
assert should_add_tour(nil, nil)
end

test "Add tour whenever Flipper is disabled " do
# Flipper is not enabled, so tour should be added regardless of options
assert should_add_tour("foo", "enabled")
assert should_add_tour("foo", "disabled")
end

test "Respect Flipper results if Flipper enabled" do
mockFlipper = Object.new
mockFlipper.stubs(:enabled?).returns(true)

Kernel.const_set('Flipper', mockFlipper)
assert_equal Flipper, mockFlipper

assert should_add_tour("foo", "enabled")
refute should_add_tour("foo", "disabled")

mockFlipper.stubs(:enabled?).returns(false)

refute should_add_tour("foo", "enabled")
assert should_add_tour("foo", "disabled")

Kernel.send(:remove_const, :Flipper)
end
end

0 comments on commit 75f4e69

Please sign in to comment.